All Prime Number

Easy
0/40
3 upvotes

Problem statement

Given an integer N, print all the prime numbers that lie in the range 2 to N (both inclusive).

Detailed explanation ( Input/output format, Notes, Images )
Input format :
 The only line of input contains a single integer N.
Output format :
Print all Prime numbers in a separate line.
Constraints
1 <= N <= 100
Sample Input 1:
9
Sample Output 1:
2
3
5
7
Sample Input 2:
20
Sample Output 2:
2
3
5
7
11
13
17
19
Approaches (1)
Brute-force method

In the outer loop, we are iterating i from 2 to n, and at the beginning of the loop set, the value of the flag to True after that in the nested loop iterate j from 2 to i check if i is divisible by j it means i has a factor and it is not a prime. Finally, check if the flag's value is True, then it is prime else, not prime.

If it is prime print i, else do nothing.

Time Complexity
Space Complexity
Code Solution
(100% EXP penalty)
All Prime Number
Full screen
Console