Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
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

Posted on November 13, 2020November 14, 2020

Table of Contents

  • Code to display prime numbers from 1 to 100 or 1 to n in Python
    • 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
      • Display prime numbers from 1 to 100 or 1 to n  using while loop
      • Display prime numbers from 1 to 100 or 1 to n using function

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

 

 

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes