Table of Contents
In this article, we will discuss the concept of Code to display prime numbers from 1 to 100 or 1 to n in Python
In this code, we are going to learn how to print prime number from 1 to 100 or 1 to n using several ways in Python language.
This is done using for loop , while loop and function in Python language.
In this program, we will print prime numbers from 1 to 100 or 1 to n using a for loop in Python language
Program 1
#Code to display prime numbers from 1 to 100 or 1 to n in Python n=int(input("Enter a maximum number for n")) for number in range(1,n+1): if(number>1): for i in range(2,number): if(number%i==0): break else: print(number)
When the above code is executed, it produces the following result
Enter a maximum number for n100 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
In this program, we will print prime numbers from 1 to 100 or 1 to n using a while loop in Python language
Program 2
#python program to print prime number from 1 to 100 or 1 to n max=int(input("Please enter any number: ")) Number=1 print("prime number between", 1, " and ", max, " are ") while(Number<=max): 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) Number=Number+1
When the above code is executed, it produces the following result
Please enter any number: 100 prime number between 1 and 100 are 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 >
In this program, we will print prime numbers from 1 to 100 or 1 to n using function in Python language
Program 3
#Python program to print prime numbers 1 to n def printPrime(n): if(n<2): return False for i in range (2,n//2+1): if(n%i)==0: return False return True max=int(input("Enter the value for range")) for x in range(1,max): if printPrime(x): print(x)
When the above code is executed, it produces the following result
Enter the value for range100 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
Suggested post
Similar post
Java programming code to check prime or not
C programming code to check prime or not
C++ programming code to check prime or not
Python programming code to check prime or not
Code to print prime numbers from 1 to 100 or 1 to n in Java
Code to print prime numbers from 1 to 100 or 1 to n in C
Code to print prime numbers from 1 to 100 or 1 to n in C++
Code to print prime numbers from 1 to 100 or 1 to n in Python
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.