First n prime numbers

# First n prime numbers

print("\n\nFirst n prime numbers")
no_of_terms = int(input("Enter number of terms : "))

if (no_of_terms > 0):
    print("The prime numbers are : ")
    num = 1
    counter = 0
    while(counter < no_of_terms):
        flag = True
        if (num >= 1 and num <= 3):
            pass
        elif ((num % 2)==0):
            flag = False
        else:
            for div in range(3, num//2, 2):
                if ((num % div) == 0):
                    flag = False
                    break
           
        if (flag == True):
            print(num)
            counter += 1
                       
        num +=1
else:
    print("Invalid number of terms")

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu