C Program to check given number Prime or not

#include<stdio.h>
main()
{
  int num,i,count=0;
  printf("Enter a number to check whether it is prime or not\n");
  scanf("%d",&num);
  for(i=1;i<=num;i++)
  {
    if(num%i==0)
    {
      count++;
    }
  }
  if(count==2)
  {
    printf("The given number %d is Prime Number\n",num);
  }
  else
  {
    printf("The given number %d is not Prime Number\n because The number is divisible by\n",num);
    for(i=1;i<=num;i++)
    {
      if(num%i==0)
      {
        printf("%d\n",i);
      }
    }
  }
}

Output:-

Enter a number to check whether it is prime or not
27
The given number 27 is not Prime Number
because The number is divisible by
1
3
9
27

Post a Comment

If you have any doubts, Please let me know
Thanks!

Previous Post Next Post