C program to find number is prime or not

If you are using any software then below program will not give an error, but if you are using TURBO C then you have to make some changes like: void main() and some function like clrscr() or getch().

#include<stdio.h>
 main()
{
 int num,div,flag;

//Accepting number
 printf("Enter the number\n");
 scanf("%d",&num);

//Checking prime or not
 if(num==2)
 {
  printf("%d is Prime Number\n",num);
 }
 else
 {
  for(div=2;div<num;div++)
  {
   if(num%div==0)
   {
    flag=0;
    break;
   }
   else
    flag=1;
  }

  if(flag==1)
   printf("%d is a Prime Number\n",num);
  else
   printf("%d is not Prime Number\n",num);
 }
}

 

OUTPUT:

Enter the number
3
3 is a Prime Number

Enter the number
8
8 is not Prime Number

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.