27-Oct-2022

.

Admin

Hi Dev,

In this short tutorial we will cover an Python Program to Print All Prime Numbers Between 1 to N. it's simple example of print n prime numbers in python. you will learn How to Display all Prime Numbers from 1 to N in Python. This post will give you simple example of Program for Prime numbers between two numbers in Python. You just need to some step to done Write a Python Program to print Prime Numbers from 1 to N using Loop.

Python program to print prime numbers from 1 to n; In this tutorial, you will learn how to print prime numbers from 1 to n (10, 100, 500, 1000) using for loop and while loop in python.

let's see below simple example with output:

**Example 1: Python Program to find Prime Number using For Loop**

Python Program to Check A given Number is Prime Or Not

Num = int(input(" Please Enter any Number: "))

count = 0

for i in range(2, (Num//2 + 1)):

if(Num % i == 0):

count = count + 1

break

if (count == 0 and Num != 1):

print(" %d is a Prime Number" %Num)

else:

print(" %d is not a Prime Number" %Num)

**Output:**

Please Enter any Number: 50

50 is not a Prime Number

**Example 2: Python Program To Print Numbers From 1 to N Using For Loop**

# Python Program to print n prime number using for loop

Number = int(input(" Please Enter any Number: "))

print("Prime numbers between", 1, "and", Number, "are:")

for num in range(1, Number + 1):

# all prime numbers are greater than 1

if num > 1:

for i in range(2, num):

if (num % i) == 0:

break

else:

print(num)

**Output:**

Please Enter any Number: 20

Prime numbers between 1 and 20 are:

2 3 5 7 11 13 17 19

**Example 3: Python Program To Print Numbers From 1 to N Using While Loop**

# Python Program to print Prime Numbers from 1 to N

max = int(input(" Please Enter Any Number: "))

Number = 1

print("Prime numbers 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, end = ' ')

Number = Number + 1

**Output:**

Please Enter Any Number: 10

Prime numbers between 1 and 10 are:

2 3 5 7

**Example 4: Python program to find sum of all prime numbers between 1 to n**

# Python Program to print n prime number using for loop

max = int(input("Find sum of prime numbers upto : "))

sum = 0

for num in range(2, max + 1):

i = 2

for i in range(2, num):

if (int(num % i) == 0):

i = num

break;

#If the number is prime then add it.

if i is not num:

sum += num

print("\nSum of all prime numbers upto", max, ":", sum)

**Output:**

Find sum of prime numbers upto : 25

Sum of all prime numbers upto 25 : 98

I hope it can help you...

#Python

- How to File Upload in Node JS using Express?
- Node JS Form Validation Example Tutorial
- Laravel str slug() helper function Example
- Python Program to Swap Two Variables Example
- How to Create Custom Validation Rules in Laravel 11?
- How to Clear Cache In Laravel 8 ?
- How to Install Discord on Ubuntu?
- Laravel str endsWith() function Example
- How to get Current Page URL in ReactJs ?
- PHP String Replace Last 3 Characters Example