21.(C).Finding prime number
#include<stdio.h>
// finding whether a number is prime or not
/* Prime number are those which are not divisible by any number except
themeselves*/
int main(){
int num,i,count=0;
scanf("%d",&num);
/* for loop will help to produce 2 or bigger natural number than 2
but not greater than num.Then,if statement will test if
the number is divisible by any number .If it's true then count wil increase
one time and leave the loop*/
for(i=2;i<num;i++)
{
if(num%i==0)
{
count++;
break;
}
} //count will be 0 if it is not divisible
if (count==0){
printf("prime\n");
}
else
{
printf("not prime\n");
}
return 0;
}
/* 2 is a prime number but it is equal to i. So, it not satisfying for the loop
condition thus it will not enter into the loop. As the count is set to
0 the count for 2 is also 0. As a result, it will enter in last if condition
and satisfying the condition it will print 2 as prime*/
Comments
Post a Comment