Write a C program to chack whether a number is prime or not?

#include<stdio.h>
#include<conio.h>
int main()
{
  int i=2, j, flag=0;
  printf("Enter a number: ");
  scanf("%d",&j);
  while(i<=j/2)
  {
      if(j%i==0)
      {
          printf("%d is not prime",j);
          flag=1; // here, flag chakes if the condition is true
          break;
      }
      else
      { i++; }
  }

  if(flag==0)
  {
      printf("%d is prine.",j);
  }
  getch();
}

Output 1:




Output 2:




Comments

Popular posts from this blog

Larch Soft Pvt. Ltd. - Best Website & Hosting Provider