Check whether the integer given is prime or composite




Prime number is a integer which have no other positive divisors other than 1 and that number itself. natural numbers other than primary number is called composite number

For example 3 is a prime number, 3 has no other positive divisors other than 1 and 3.  

In this c program we are finding out a number is prime or not by using mod operator. First we are checking the number is greater than 1, if not the c program gets exit displaying number is not prime. else we enters a for loop from 1 to that number, in that loop we check if( ( num % j ) == 0) if it is true we prints number is not prime. else the given integer is prime.

===================================================================
#include <stdio.h>
 
#include <stdlib.h>
 
#include <conio.h>
 
void main()
 
{
	int num, j, flag;
 
	clrscr();
 
	printf("Enter a number\n");   /* take th input number fro user */
 
	scanf("%d", &num);
 
  	if ( num <= 1)
 
	{
     		printf("%d is not a prime numbers\n", num);   /* check whether number is less than 1 */
 
	  	exit(1);
 
   	}
 
	flag = 0;
 
   	for ( j=2; j<= num/2; j++)
 
	{
 
		if( ( num % j ) == 0)          /* do the calculation using mod operator and check the given number is prime or not  */
 
		{
 
         		flag = 1;
 
			break;
 
		}
 
	}
 
     	if(flag == 0)
 
		 printf("%d is a prime number\n",num);
 
     	else
 
		 printf("%d is not a prime number\n", num);         /* prints the output of the program */
 
}
==================================================================
OUTPUT of C program to check number is prime or not
==================================================================
Output
RUN 1
Enter a number
34
34 is not a prime number

RUN 2
Enter a number
29
29 is a prime number