Prime Number

Easy
0/40
5 upvotes
Asked in companies
CognizantShareChatTata Consultancy Services (TCS)

Problem statement

Write a Program to check whether the given number N is prime or not.

For a given number N check if it is prime or not. A prime number is a number that is only divisible by 1 and itself.
Detailed explanation ( Input/output format, Notes, Images )
Input format :
The only line of input contains a single integer N.
Output Format :
The only line of the output prints either true or false.
Constraints :
0 <= N <= 10,000
Sample Input 1 :
5
Sample Output 1 :
true
Sample Input 2 :
10
Sample Output 2 :
false
Explanation of Input 2:
10 has 2 factors 2, 5.
Approaches (1)
Brute-force method

In the program, a for loop is iterated from i = 2 to i < n.

In each iteration, whether n is perfectly divisible by i is checked using:

if (n % i == 0)

If n is perfectly divisible by I, n is not a prime number. In this case, the flag is set to False.

After the loop, if n is a prime number, the flag will still be True. However, if n is a non-prime number, the flag will be False.

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