Write a C Program to Check whether the Given Number is a Prime Number

Write a C Program to Check whether the Given Number is a Prime Number.

#include
#include
void main()
{
	int num, i, flag;
	clrscr();
	printf("Enter a number \n");
	scanf("%d", &num);
	if (num <= 1)
	{
		printf("%d is not a prime number. \n", num);
	}
	else
	{
		flag = 0;
		for (i = 2; i <= num / 2; i++)
		{
		   if ((num % i) == 0)
		   {
			  flag = 1;
			  break;
		   }
		}
		if (flag == 0)
			printf("%d is a prime number. \n", num);
		else
			printf("%d is not a prime number. \n", num);
	}
	getch();
}

Examples

Solution to check whether the given number is a prime number in C programming language.