Python Program to print Prime Numbers from 1 to 100

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, 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.

# 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 Prime Numbers from 1 to 100 output

 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 Loop, While loop, Prime Number, if statement, and break statement article to understand the Python logic.

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 Prime Numbers from 1 to 100 using a while loop output

 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  

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 100 3

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 Prime Numbers from 1 to N using a while loop output

 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 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 Sum fo Prime Numbers from 1 to N output

 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 of Prime Numbers from 10 to 150 = 2259

This Python Program allows the user to enter Minimum and Maximum values and find the Sum of Prime Numbers from 1 to N. 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))

Python Prime Numbers sum output

 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 of Prime Numbers from 1 to 100 = 1060