Python Program to Print Prime Numbers from 1 to N using For loop
ADVERTISEMENTS
Python program to print prime numbers from 1 to n using for loop. In this article, you will learn how to make python program to print prime numbers from 1 to n using for loop.
Sample of Prime Numbers
2 3 5 7 .......... 67 71 73
Source Code
# Python Program to Print Prime Numbers from 1 to N using For loop
print ("Please enter a range for print the prime numbers: ", end="")
x = int (input ())
print ("\n\n------The prime numbers from 1 to ", x, " are------\n\n")
for i in range (x):
# There are neither prime nor composite if as skip 0 and 1 number
if i == 1 or i == 0:
continue
f = 1
for j in range (2, int (i / 2) + 1):
if i % j == 0:
f = 0
break
# If f = 1 means i is prime number and f = 0 means i is not prime number
if f == 1:
print (i, end=" ")
print ("\n")
Output
Please enter a range for print the prime numbers: 75
------The prime numbers from 1 to 75 are------
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73