#n=int(input("Enter a number : "))
n=50 # change this value
for i in range(2,n+1):
flag=0
for j in range(2,int(i/2)+1):
if(i%j==0):
flag=1
break
if(flag==0):
print(i,end=', ')
Output
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
In above code we can remove the comment at first line and add comment to 2nd line to ask the user to input a number. By default all inputs are string so we need to convert this to integer by using int() function. n=1
i=2
while ( n <= 10): # change this value for more numbers
b=0
for j in range(2,int(i/2)+1):
if(i%j==0):
b=1
break
if(b==0):
print(i,end=', ')
n=n+1
i=i+1
Output
2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
n=int(input("Enter number :"))
for i in range(2,n):
if(n%i==0):
print("composite/Not a prime number")
break
else:
print("prime number")
One more way to generate Prime numbers
num=100 # Upper limit of prime numbers
i=2
while i<=num-1:
j=2 # start checking each number from 2
while j <= i/2 : # check upto half the number
if(i%j==0): # reminder of division is 0
break # come out of loop without else part
j=j+1
else:
print(i, " is a prime number") # if break is not encountered
i=i+1

Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.