C Program to Check whether a number is Prime Using Function

Problem: Write a C program to check whether given number is prime or not using function.

Solution: This C program accepts a number from user and passes it to a function named isPrime() which checks whether the input number is prime or not. Any natural number which is divisible by 1 and itself is known as prime number. So in isPrime() function we are using for loop to check if given number is divisible by any number between the range (1, number), if it divisible then it is not a prime number else it is a prime number.
/* Aim: Write a function for the function prototype void isPrime(int n)*/

#include<stdio.h>
#include<stdlib.h>

void isPrime(int n); // isPrime Function Prototype

int main()
{
	int n;

	printf("\n Enter any number:- ");
	scanf("%d",&n);

	isPrime(n);

	 return 0;
}
// isPrime Function
void isPrime(int n)
{
	int i,LED=0; 

	if(n<2)
	printf("\n %d is not prime number \n \n",n);
	exit(1);
   
	for(i=2;i<=n/2;++i)
	{
	if(n%i==0)
	LED=1;
	break;
	}

	if(LED==0)
		:printf("\n %d is prime number \n \n",n);
	else
		:printf("\n %d is not prime number \n \n",n);
}

Output:

[root@localhost ~]# gcc -o isPrime isPrime.c
[root@localhost ~]# ./isPrime

 Enter any value:- 2

 2 is prime number