Python Program to Print Prime Number From 1 to N

Python Program to Print Prime Number From 1 to N

In this tutorial, we will make 3 to 4 simple programs with the help of which we will print prime numbers from 1 to N (10, 100, 500, 1000) and also print their sum using for loop and while loop in python.

Python Program to Print Prime Number From 1 to N(10, 100, 500, 1000)

Let’s see the following program to print prime numbers from 1 to N (10, 100, 500, 1000, etc) using for loop, while loop:

  • Python Program to find Prime Number using For Loop
  • Python Program To Print Numbers From 1 to N Using For Loop
  • Python Program To Print Numbers From 1 to N Using While Loop
  • Python program to find sum of all prime numbers between 1 to n

Python Program to find Prime Number using For Loop

Python Program to Check A given Nober is Prime Or Not
 No = int(input(" Please Enter any Nober: "))
 flag = 0
 for i in range(2, (No//2 + 1)):
     if(No % i == 0):
         flag = flag + 1
         break
 if (flag == 0 and No != 1):
     print(" %d is a Prime Nober" %No)
 else:
     print(" %d is not a Prime Nober" %No)

Output

Please Enter any Number:  50  
50 is not a Prime Number 

Python Program To Print Numbers From 1 to N Using For Loop

# Python Program to print n prime No using for loop
 
No = int(input(" Please Enter any No: "))

print("Prime Nos between", 1, "and", No, "are:")

for Nomber in range(1, No + 1):
   # all prime Nos are greater than 1
   if Nomber > 1:
       for i in range(2, Nomber):
           if (Nomber % i) == 0:
               break
       else:
           print(Nomber)
    
   

Output

Please Enter any Number:  25 
Prime numbers between 1 and 25 are: 
2 3 5 7 11 13 17 19 23 

Python Program To Print Numbers From 1 to N Using While Loop

# Python Program to print Prime NOMBERs from 1 to N
 
max = int(input(" Please Enter Any NOMBER: "))

NOMBER = 1

print("Prime NOMBERs between", 1, "and", max, "are:")

while(NOMBER <= max):
    Flg = 0
    i = 2
    
    while(i <= NOMBER//2):
        if(NOMBER % i == 0):
            Flg = Flg + 1
            break
        i = i + 1

    if (Flg == 0 and NOMBER != 1):
        print(" %d" %NOMBER, end = '  ')
    NOMBER = NOMBER  + 1a

Output

Please Enter Any Number:  15 
Prime numbers between 1 and 15 are:  
2   3   5   7   11   13  

 

Python program to find sum of all prime numbers between 1 to n

# Python Program to print n prime No using for loop
 
max = int(input("Find sum of prime No upto : "))

sum = 0

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

    i = 2
    
    for i in range(2, Nomber):
        if (int(Nomber % i) == 0):
            i = Nomber
            break;

    #If the Nomberber is prime then add it.
    if i is not Nomber:
        sum += Nomber

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

Output

Find sum of prime numbers upto :  25 
Sum of all prime numbers upto 25 : 98   

Conclusion

Through this tutorial, we have learned how to print prime numbers and it’s sums from 1 to N using for loop, while loop in python.

List of Simple Python Programs

  1. Python Program to Add Two Numbers
  2. Python Program to Find/Calculate Sum of n Numbers
  3. Python Program to Find/Calculate Average of 3, 4, 5…n numbers
  4. Python Program to Print ASCII Value of Character
  5. Write a Program to Calculate Simple Interest in Python
  6. Python Program to Compute Compound Interest
  7. Leap Year Program in Python
  8. Python Program to Print Star Pattern
  9. Number Pattern Programs in Python
  10. Python Program to Print Even and Odd numbers From 1 to N
  11. Python Abs() Function: For Absolute Value
  12. How to Check Whether a Number is Fibonacci or Not in Python
  13. Python: Program to Find Power of Number
  14. Python Program to Reverse a Numbers
  15. Python Program to Find Smallest/Minimum of n Numbers
  16. Python Program to Find Largest/Maximum of n Numbers
  17. Python Program to Find The Net Bill Amount After Discount
  18. Python Program to Print Numbers From N to 1 and 1 to N
  19. Python Program to Print Numbers Divisible by 3, 5, 7
  20. How to Find Square of Number in Python
  21. Python Program to Calculate Cube of Number
  22. Python Program to Find LCM of Two Numbers
  23. BMI (Body Mass Index) Calculator in Python
  24. Palindrome Program in Python using while loop, Function, etc
  25. Python: Program to Count Total Number of Bits in Number
  26. Python Random Number Generator Code
  27. Python Program to Calculate n-th term of a Fibonacci Series
  28. Zip Zap Zoom Python Program
  29. Python: program to convert Celsius to Fahrenheit
  30. Python Program to Swap Two Numbers
  31. Python Program to Get Standard Deviation
  32. Python Program to Find the Variance
  33. Python Program to Convert Height in cm to Feet and Inches
  34. Python Program to Convert Meters into Yards, Yards into Meters
  35. Python Program to Convert Kilometers to Meters, Miles
  36. Python Program to Find Perfect Number
  37. Python: Program to Find Strong Number
  38. Python Program Create Basic Calculator
  39. Python Program For math.floor() Method
  40. Python Program to Find Sum of Series 1/1! 2/2! 3/3! …1/n!

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *