BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C Program to Check Whether a Number is Prime or Not

Last Updated: July 15, 2022 by Chaitanya Singh | Filed Under: C Programs

In this tutorial, you will learn how to write a C program to check whether a number is prime or not.

A positive number is called prime number if it is divisible by 1 and itself. For example: 13, 19, 23 are prime numbers because they are divisible by 1 and themselves.

Program to check whether a number is prime or not

In this example, we are checking whether the number entered by user is a prime number or not.

The first check we are doing is by comparing the input number with 0 and 1, if the number is either 0 or 1, it is not a prime number.

The second check we are doing here is checking whether the input number is divisible by any number between 2 to num/2, if it is then its not a prime number else it is a prime number.

#include <stdio.h>

int main() {

  //Here flag value 0 means prime number and
  //1 means non-prime number
  int num, i, flag = 0;
  printf("Enter a number to check whether prime or not: ");
  scanf("%d", &num);

  // 0 and 1 are not the prime numbers so setting
  // the flag to 1 if entered number is 0 or 1
  if (num == 0 || num == 1)
  flag = 1;

  for (i = 2; i <= num / 2; ++i) {

    // if num is divisible by i, then num is not prime
    // change the flag value to 1
    if (num % i == 0) {
      flag = 1;
      break;
    }
  }

  // If flag is 0 it means the number is prime
  if (flag == 0)
    printf("Entered number %d is a prime number.", num);
  else
    printf("Entered number %d is not a prime number.", num);

  return 0;
}

Output: User enters the number 13, which is a prime number so the program prints the message that “Entered number 13 is a prime number”.
C Program to Check Whether a Number is Prime or Not

Related C Examples:

  • C Program to display characters from ‘A’ to ‘Z’ using loop
  • C Program to find LCM of two numbers
  • C Program to find GCD of two numbers
  • C Program to convert decimal to octal number
❮ C TutorialC Programs ❯

Top Related Articles:

  1. C Program to check Armstrong number
  2. C Program to concatenate two strings without using strcat
  3. C Program to validate a given date
  4. C Program to Check whether an Alphabet is Vowel or Consonant
  5. C Program to check Leap Year

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap