C program to check whether a number is prime number or not

Write a program in C to enter any number and check whether the number is prime number or not using for loop. How to check prime numbers using loop in C programming. Logic to check prime numbers in C program.

Example

Input

Input any number: 17

Output

17 is prime number

Required knowledge

Basic C programming, If else, For loop

Must know - Program to check divisibility

What is Prime number?

Prime numbers are the positive integers greater than 1 that is only divisible by 1 and self. For example: 2, 3, 5, 7, 11 etc...

Logic to check prime number

There are several advanced and efficient algorithms for prime test. I have implemented the simplest and easiest algorithm, for beginners. I am sure that you must have implemented this logic in your real life to check primes. So here goes the logic in brief, if the number is divisible by any number in between 2 to n-1. Then it is composite number not prime otherwise prime.

Below is the step by step descriptive logic to check prime number.

  1. Read a number from user. Store it in some variable say num.
  2. Initialize another variable say isPrime. isPrime variable is used as a notification or flag variable. It contain either 0 or 1. Assigning 0 means number is composite and 1 means prime.
  3. Initially assume that the number is prime. For that set the isPrime = 1.
  4. Run a loop from 2 to num/2, increment 1 in each iteration. The loop structure should be like for(i=2; i<=num/2; i++).
  5. Check, if the num is divisible by i i.e. if(num % i == 0). Then the number should be marked as composite i.e. isPrime = 0 and exit from loop.
  6. Outside the loop check the current value of isPrime variable. According to our assumption if it is equal to 1 then the number is prime otherwise composite.

Program to check prime number

/**
 * C program to whether a number is prime Number or not
 */

#include <stdio.h>

int main()
{
    int i, num, isPrime;

    /*
     * isPrime is used as notification. Initially I 
     * have supposed that the number is prime.
     */
    isPrime = 1; 

    /* Read a number from user */
    printf("Enter any number to check prime: ");
    scanf("%d", &num);

    for(i=2; i<=num/2; i++)
    {
        /* 
         * If the number is divisible by any number 
         * other than 1 and self then it is not prime
         */
        if(num%i==0)
        {
            isPrime = 0;
            break;
        }
    }

    /*
     * If isPrime contains 1 then it is prime
     */
    if(isPrime ==1)
    {
        printf("%d is prime number", num);
    }
    else
    {
        printf("%d is composite number", num);
    }

    return 0;
}

Enhance your skills by learning this program using another approach.

Read more - Program to check prime using functions.

Output

Enter any number to check prime: 7
7 is prime number

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>