In this post, we are going to write python program to print prime numbers from 1 to 100. We can use for loop ,while loop to print prime numbers.
Lets see what is prime number?
A prime number is a natural number which is greater than 1 and has no positive divisor other than 1 and itself, such as 2, 3, 5, 7, 11, 13, and so on.
Python Program to print Prime Numbers from 1 to 100 using nested for loop
In this program,we will use nested for loop to print prime number between 1 to 100. We will use first for loop to iterate between 1 to 100 values. Second for loop will be used to check whether number is divisible or not and also Check for each number if it has any factor between 1 and itself. 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 number from 1 to 100 using nested for loop
# python program to print prime number from 1 to 100
print("List of prime numbers from 1 to 100 :")
for n in range (1, 101):
count = 0
t = n//2
for i in range(2, (t + 1)):
if(n % i == 0):
count = count + 1
break
if (count == 0 and n > 1):
print(" %d" %n, end = ' ')
Output:
List of prime numbers from 1 to 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
Python program to print sum of prime number from 1 to 100 using nested for loop
In this program ,we will calculate sum of prime number from 1 to 100 using nested for loop.
Python program to print sum of prime number from 1 to 100 using nested for loop
# python program to print prime number from 1 to 100
print("List of prime numbers from 1 to 100 :")
sum = 0
for n in range (1, 101):
count = 0
t = n//2
for i in range(2, (t + 1)):
if(n % i == 0):
count = count + 1
break
if (count == 0 and n > 1):
print(" %d" %n, end = ' ')
sum = sum + n
print("\n\nSum from 1 to 100 = %d" %(sum))
Output:
List of prime numbers from 1 to 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 from 1 to 100 = 1060
Python program to print prime number from 1 to 100 using nested while loop
n = 1
print("List of prime number from 1 to 100 :")
while(n <= 100):
count = 0
i = 2
t = n//2
while(i <= t):
if(n % i == 0):
count = count + 1
break
i = i + 1
if (count == 0 and n != 1):
print(" %d" %n, end = ' ')
n = n + 1
Output :
List of prime number from 1 to 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
Python program to print sum of prime number from 1 to N using nested for loop and ask user to enter minimum and maximum range values.
In this program, The user is given two integer numbers, lower value, and upper value. The task is to write the Python program for printing all the prime numbers between the given interval (or range) and also calculate sum of those prime numbers
low = int(input("Enter the lowest Value: "))
high = int(input("Enter the highest Value: "))
sum = 0
for n in range (low, high + 1):
count = 0
t = n//2
for i in range(2, (t + 1)):
if(n % i == 0):
count = count + 1
break
if (count == 0 and n != 1):
print(" %d" %n, end = ' ')
sum = sum + n
print("\n\nSum from %d to %d = %d" %(low,high,sum))
Output:
Enter the lowest Value: 20 Enter the highest Value: 500 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 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 Sum from 20 to 500 = 21459
Python program to display prime number in given range
In this program, The user is given two integer numbers, lower value, and upper value. The task is to write the Python program for printing all the prime numbers between the given interval (or range)
low = int(input("Enter the lowest Value: "))
high = int(input("Enter the highest Value: "))
for n in range (low, high + 1):
count = 0
t = n//2
for i in range(2, (t + 1)):
if(n % i == 0):
count = count + 1
break
if (count == 0 and n != 1):
print(" %d" %n, end = ' ')
Output:
Enter the lowest Value: 10 Enter the highest Value: 400 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 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397