programming-examples/c/Functions/C Program to Check if a given Number is Prime number.c

31 lines
723 B
C
Raw Normal View History

2019-11-15 12:59:38 +01:00
/*
* C program to check whether a given number is prime or not
* and output the given number with suitable message.
*/
#include <stdio.h>
#include <stdlib.h>
void main()
{
int num, j, flag;
printf("Enter a number \n");
scanf("%d", &num);
if (num <= 1)
{
printf("%d is not a prime numbers \n", num);
exit(1);
}
flag = 0;
for (j = 2; j <= num / 2; j++)
{
if ((num % j) == 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);
}