Write a Python Program to print Prime numbers from 1 to 100, or 1 to n, or minimum to maximum with examples and also calculate the sum of them.
Python Program to print Prime Numbers from 1 to 100 using For Loop
This Python program prints the prime numbers from 1 to 100 using for loop and break. 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 was divisible or not. If true, the count is incremented, and the break statement skips that number.
Next, the 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.
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 = ' ')
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
I suggest you refer to the For, While, Prime Number, if statement, and break statement articles to understand the Python logic.
Instead of madly printing them 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.
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 100 using While Loop
In this example, we just replaced the For loop in the above Prime Numbers example with the While loop.
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
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
This program to display prime numbers from 1 to N is the same as above. We replaced the For loop with While Loop.
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
Please Enter the Minimum Value: 100
Please Enter the Maximum Value: 250
101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241
Python Program to Print & return Sum of Prime Numbers from 1 to 100
This program finds the prime numbers between 1 and 100, and it will add those values to find the sum.
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 from %d to %d = %d" %(minimum, maximum, total))
Please Enter the Minimum Value: 10
Please Enter the Maximum Value: 150
11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149
Sum from 10 to 150 = 2259
This Python Program allows users to enter Minimum and Maximum values and find the Sum. Next, it returns the sum of prime numbers between Minimum and Maximum values.
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 = %d" %total)
Please Enter the Minimum Value: 1
Please Enter the Maximum Value: 100
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Sum = 1060