C Program to Check the number is prime or not

Write a C Program to check whether the number is prime or not?

In our last program, we saw how to calculate profit and loss in c programming. In this program, we will use an if-else statement to check whether the number is prime or not.
A prime number is a positive integer which is divisible only by 1 and itself. For example: 2, 3, 5, 7, 11, 13.When a number has more than two factors then it is called a composite number.

This program takes the number  from the user and then checks whether the input number is prime or not. Then the program displays the result.

C programming code:

#include<stdio.h>
int prime(int num)
{
int i,c=0;
for(i=1;i<=num;i++)
{
if(num%i==0)
{
c++;
}
}
if(c==2)
{
printf(“%d num is prime\n”,num);
}
else
{
printf(“%d num is not prime\n”,num);
}
return (num);
}
int main()
{
int a;
printf(“enter the value of a”);
scanf(“%d”,&a);
prime(a);
printf(“a=%d”,a);
return 0;
}

Let us know if you are stuck somewhere and need help.We will try our best to provide you a solution to your problem. You are also free to request some other problem using our comments section.