prime or not

Code to display prime numbers from 1 to 100 or 1 to n in Python

Code to display prime numbers from 1 to 100 or 1 to n in Python

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.

Program to display prime numbers from 1 to 100 or 1 to n

Display prime numbers from 1 to 100 or 1 to n using for loop

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

 

Display prime numbers from 1 to 100 or 1 to n  using while loop

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
>

 

Display prime numbers from 1 to 100 or 1 to n using function

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

Python function

For loop in Python

while loop in Python

Operator in Python

Data types in Python

 

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

 

 

 

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.