Sunday 28 February 2016

Search prime number

Q. Write a C program to find whether a number is prime or not.


A prime number is one,which is divisible only by one or itself. And its must greater than 1.

Ans.


#include<stdio.h>
#include<stdio.h>
int main()
{
 int x,num;
 printf("Enter number : ");
 scanf("%d",&num);
 x=2;
 while(x<=num-1)
 {
   if(num%x==0)
   {
      printf("Number is not prime!!");
      break;
   }
   x++;
 }
 if(x==num)
    printf("Number is prime!!");
 getch();
 return 0;
}

Output:


Enter number : 7
It is prime!!




No comments:

Post a Comment

Ads Inside Post