9.27.2011

search prime number

Q. Write a C program to find whether a number is prime or not.
Definition of prime number:A prime number is one,which is divisible only by one or itself. And its must greater than 1.
And if number is not prime, it is called composite number and vice versa.

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 of above program :

Enter number : 7
Number is prime!!


Related Programs:

  1. Generate first n Prime number C program
  2. Print Prime Number 1 to 100 C program
  3. Flowchart for search prime number

1 comment:

  1. It is not needed to check up to num-1 you can check up to square root of num is enough which simplifies and efficient program

    ReplyDelete