Problem no 13: check whether the number is prime or not.

 QUESTION:

 For a given number N check if it is prime or not. A prime number is a number which is only divisible by 1 and itself.
 

Example 1:

Input:
N = 5
Output:
1
Explanation:
5 has 2 factors 1 and 5 only.

Example 2:

Input:
N = 25
Output:
0
Explanation:
25 has 3 factors 1, 5, 25 
 
ANSWER:
 
int isPrime(int n){
if(n==1)
{
return 0;
}
else
{
for(int i=2;i<=n/2;i++)
{
if(n%i==0)
{
return 0;
}
}
return 1;
} 



Comments

Popular posts from this blog

problem 3: given two integers N and M. The problem is to find the number closest to N and divisible by M. If there are more than one such number, then output the one having maximum absolute value.

problem no 7:Given two numbers A and B, find Kth digit from right of AB.

Problem no 16: count the number of squares below N.