Home C Language Basic C Programs Prime number in C

Prime number in C

Write a C Program to Find Whether the Given Number is Prime Number or not.

#include<stdio.h>

int main()

{

   int a,i,count=0; //’a’ is user defined number, ‘i’ for iteratiions, ‘count’ for counting factor of given number.

   printf(“Enter a number :”);

   scanf(“%d”,&a);

   for(i=1;i<=a;i++)

   {

      if(a%i==0) //check the iteration value is factor of given number or not.

         count++;

   }

   if(count==2) //Every prime number should have only two factors.

      printf(“Given number is PRIME number\n”);

   else

      printf(“Given number is Non-PRIME number\n”);

   return 0;

}

Output :

i) Enter a number : 28541

Given number is PRIME number

ii)Enter a number : 30857

Given number is Non-PRIME number

For improvement of your coding skills we give some tasks along this. If you solve this tasks and send to our email (onlineexamshubteam@gmail.com) with your details. we will display your details(like Name, City, college, photo) in our site.

Task : Write a c program to print nth prime number.