C program that receives an integer number and determines if it is a prime number

Posted by Samath
  1096
June 01, 2015

 

Write a C program that receives an integer number and determines if it is a prime number or not.

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


bool isPrime(int n);

int main(int argc, char *argv[])
{
	int num; 
	printf("Please enter a number: "); 

	scanf("%d", &num); 
	
	if(isPrime(num))
	{
		printf("%d is a prime number\n",num);
	}
	else
	{
		printf("%d is NOT a prime number\n",num);
	}

}

bool isPrime(int n)
{
	long i; 
	if (n < 2) 
		return false; 
		
	for (i = 2; i < n; i++) 
	{
		if ((n % i) == 0) 
			return false;
	}	
	return true; 
}