C Program to check whether the Integer given is prime or composite

A number is called a prime number if it has only 2 divisors which are 1 and that number itself. All other numbers other than prime numbers are composite numbers. For a better understanding of this prime number check C program example, we always recommend you to learn the basic topics of C programming listed below:

What are prime numbers and composite numbers?

To check whether the number given is prime or composite using a C program, we must understand about prime number concept and composite numbers.

The Prime number is an integer with no other positive divisors other than '1' and that number itself. For example, '7' is a prime number because '7' has only 2 divisors that are '1' and '7' only. Natural numbers other than the prime number are called the composite number. For example, the number '4', and '6', which have more than two divisors are composite numbers. 

Note:  the number '1' is not a prime number as it has only one divisor.

How to check prime number in C

Here we write a C program for finding out whether 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 exits, displaying the number is not Prime. else we enter 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.

ALGORITHM

STEP 1: Include the Header files to use the built-in functions in the C program.

STEP 2: Declare and Initialize the variables as an integer Data type.

STEP 3: Accept the number from the user using printf and scanf built-in functions.

STEP 4: Check the number is less than one using an 'if' condition; if so, display not a Natural number and Exit the program.

STEP 5: Open a 'for loop' from 2 to half of the number and Increment the 'j' by one. ( we have Divisors up to half of the number only]

STEP 6: Check the number Mod 'j' is zero; if so, we Assign a Flag variable as one and Break the loop.

STEP 7: Check the Flag using an 'if' condition as if the Flag is Zero, print its a Prime number else it's a Composite number.

C Source Code

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


void main()
    {
        int num, j, flag;
        printf("Enter a number\n");                                 /* take the input number from 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 composite number\n", num);         /* prints the output of the program */
    }
                                      

OUTPUT

Output

RUN 1

Enter a number
34
34  is not a composite number

RUN 2

Enter a number
29
29 is a prime number