C Programming Tutorial

Advertisements

Write program to check whether a number is prime or not in c



       #include<stdio.h>
       #include<conio.h>
       void main()
       {
              int num,i,temp=0; 
              clrscr();

              printf("\nEnter any number : "); 
              scanf("%d",&num);
              
              for(i=2;i<=num/2;i++)
              {
                     if(num%i == 0)
                     {
                           temp=1;
                           break;
                      }    
              }
              
              if(temp==0)
                     printf("%d is prime number.");
              else
                     printf("%d is not prime number.");              

              getch();
       }

   Output : 

              Enter any number : 17
               17 is prime number.




Advertisement