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
Post a Comment