Search This Blog

Friday, October 7, 2011

Write a program to find if the Number is Prime?

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  #include<math.h>
   4:  void main()
   5:  {
   6:  int k,n,r;
   7:  int prime;
   8:  printf("Enter the num you want to test:");
   9:  scanf("%d",&n);
  10:  k=2;
  11:  while(k<=sqrt(n))
  12:  {
  13:   r=n%k;
  14:   if(r==0)
  15:   {
  16:   prime=0;
  17:   break;
  18:   }
  19:   else
  20:   k++;
  21:  }
  22:  if(prime==0)
  23:  printf("%d is not a prime num: ",n);
  24:  else
  25:  printf("%d is a prime num:",n);
  26:  getch();
  27:  }

No comments:

Post a Comment