You can mail me Contact Me!

Program that prints prime numbers less than N

Program that prints prime numbers less than N

 Algorithm:

  1. Prompt the user to enter a range N.
  2. Print the title "Prime numbers less than N".
  3. Iterate through all numbers from 2 to N-1.
  4. For each number num: a. Assume num is prime. b. Check if num is divisible by any number from 2 to the square root of num. c. If num is divisible by any number, set is_prime to False and break out of the inner loop. d. If is_prime is still True, print num.

Hello, dear reader! 👋

Thanks for visiting my blog! I’m a student just like you, sharing what I learn to help others with Python programming. I hope my posts are useful for your studies! 😊

If you find this post helpful, please leave a comment—even just a few emojis will make my day! 🐍✨ Your feedback keeps me motivated to create more content for everyone. 🚀

Happy programming!
Abhin Krishna, S01, EB Department, MEC


Pseudocode:

START
DISPLAY "Enter a range: "
INPUT N
DISPLAY "Prime numbers less than", N
FOR num FROM 2 TO N - 1
    SET is_prime = TRUE
    FOR i FROM 2 TO SQUARE_ROOT(num)
        IF num is divisible by i
            SET is_prime = FALSE
            BREAK
    IF is_prime
        PRINT num
END
Program:

n = int(input("Enter a range: "))
print(f"Prime numbers less than {n}:")

for num in range(2, n):
    is_prime = True
    for i in range(2, int(num ** 0.5) + 1):
        if num % i == 0:
            is_prime = False
            break
    if is_prime:
        print(num, end=' ')  
Flowchart:
flowchart TD A([Start]) --> B[/Display "Enter a range: " /] B --> C[/Input N/] C --> D[/Display "Prime numbers less than", N/] D --> E[num = 2] E --> F[is_prime = true] F --> G[i = 2] G --> H{num % i == 0?} H -- Yes --> I[is_prime = false] H -- No --> J[i = i + 1] I --> K{i <= sqrt num ?} K -- Yes --> G K -- No --> L[/Print num/] L --> M[num = num + 1] M --> N{num < N?} N -- Yes --> F N -- No --> O([End])

Flowchart Image:


Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
Site is Blocked
Sorry! This site is not available in your country.