Friday, February 3, 2017

WAP TO FIND ENTERED NUMBER IS PRIME NUMBER OR NOT


/ * C program to find the Number is Prime or not-Prime */ 
           /*Program By Laxman Poudel*/
#include<stdio.h>
main()
{
 int n,r=1,d=2;
 printf("\n Enter the Number : ");
 scanf("%d",&n);
 if(n==2)
 {
   printf("\n %d is a Prime Number \n",n);
   exit(0); //to exit from program
 }
 r=n%d; /*we have to do this before while also becuuse
 if while condition fails then r=n%d inside while is not executed*/

 while(d<=n/2)
 {
  r=n%d;
  if(r==0)
  break; //if r==0, then to come out of while loop
  d++; //incrementing d value if(r==0) fails..
}
if(r==0||n==1) // since 1 is not a prime number
printf("\n %d is not a Prime Number \n",n);
else
printf("\n %d is a Prime Number \n",n);
return(0);

}                           OUTPUT OF THE PROGRAM


No comments:

Post a Comment