Algorithm:
- Prompt the user to enter a range
N
. - Print the title "Prime numbers less than N".
- Iterate through all numbers from 2 to
N-1
. - For each number
num
: a. Assumenum
is prime. b. Check ifnum
is divisible by any number from 2 to the square root ofnum
. c. Ifnum
is divisible by any number, setis_prime
toFalse
and break out of the inner loop. d. Ifis_prime
is stillTrue
, printnum
.
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: