Write a Python Program to print Prime numbers from 1 to 100, or 1 to n, or minimum to maximum with example. In this Python program, we also calculate the sum of prime numbers from 1 to n.
Python Program to print Prime Numbers from 1 to 100 using For Loop
This python program display the prime numbers from 1 to 100.
First, we used For Loop to iterate a loop between 1 and 100 values. Within the for loop,
- We used another For Loop to check whether the number is divisible or not. If true, count incremented, and break statement skip that number.
- Next, if statement checks whether the count is zero, and the given number is not equal to 1. If it is true, it prints the number because it is a Prime Number.
TIP: I suggest you refer to the Prime Number article to understand the Python logic.
# Python Program to print Prime Numbers from 1 to 100 for Number in range (1, 101): count = 0 for i in range(2, (Number//2 + 1)): if(Number % i == 0): count = count + 1 break if (count == 0 and Number != 1): print(" %d" %Number, end = ' ')
Python Program to print Prime Numbers from 1 to 100 using While Loop
We just replaced the For loop in the above Python Prime Numbers example with While loop.
# Python Program to print Prime Numbers from 1 to 100 Number = 1 while(Number <= 100): count = 0 i = 2 while(i <= Number//2): if(Number % i == 0): count = count + 1 break i = i + 1 if (count == 0 and Number != 1): print(" %d" %Number, end = ' ') Number = Number + 1
Python Program to print Prime Numbers from 1 to N using For Loop
Instead of madly printing prime numbers from 1 to 100, this python program allows users to enter the minimum and maximum values. Next, it prints prime numbers between Minimum and Maximum values.
# Python Program to print Prime Numbers from 1 to N minimum = int(input(" Please Enter the Minimum Value: ")) maximum = int(input(" Please Enter the Maximum Value: ")) for Number in range (minimum, maximum + 1): count = 0 for i in range(2, (Number//2 + 1)): if(Number % i == 0): count = count + 1 break if (count == 0 and Number != 1): print(" %d" %Number, end = ' ')
Python Program to print Prime Numbers from 1 to N using While Loop
This program to display prime numbers from 1 to N is the same as above. We replaced the For loop with While Loop.
# Python Program to print Prime Numbers from 1 to N minimum = int(input(" Please Enter the Minimum Value: ")) maximum = int(input(" Please Enter the Maximum Value: ")) Number = minimum while(Number <= maximum): count = 0 i = 2 while(i <= Number//2): if(Number % i == 0): count = count + 1 break i = i + 1 if (count == 0 and Number != 1): print(" %d" %Number, end = ' ') Number = Number + 1
Python Program to return Sum of Prime Numbers from 1 to 100
This program finds the prime numbers between 1 and 100. Next, it’s going to add those numbers to find the sum of prime numbers between 1 and 100.
# Python Program to find Sum of Prime Numbers from 1 to N minimum = int(input(" Please Enter the Minimum Value: ")) maximum = int(input(" Please Enter the Maximum Value: ")) total = 0 for Number in range (minimum, maximum + 1): count = 0 for i in range(2, (Number//2 + 1)): if(Number % i == 0): count = count + 1 break if (count == 0 and Number != 1): print(" %d" %Number, end = ' ') total = total + Number print("\n\nSum of Prime Numbers from %d to %d = %d" %(minimum, maximum, total))
Python Program to find Sum of Prime Numbers from 1 to N
It allows the user to enter Minimum and Maximum values. Next, Python returns the sum of prime numbers between Minimum and Maximum values
# Python Program to print Prime Numbers from 1 to N minimum = int(input(" Please Enter the Minimum Value: ")) maximum = int(input(" Please Enter the Maximum Value: ")) total = 0 Number = minimum while(Number <= maximum): count = 0 i = 2 while(i <= Number//2): if(Number % i == 0): count = count + 1 break i = i + 1 if (count == 0 and Number != 1): print(" %d" %Number, end = ' ') total = total + Number Number = Number + 1 print("\n\nSum of Prime Numbers from %d to %d = %d" %(minimum, maximum, total))