Skip to content

C Program to Check Prime Number

Category
Programming
On this page

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. The first few prime numbers are 2, 3, 5, 7, 11, and so on. To check if a number is prime or not, we can use the concept of loop structures in C programming.

In this article, we will explore a C program to check if a number is prime.

Understanding the Logic

The logic of the program is quite straightforward. Here is the procedure to follow:

  1. Take an input number from the user.
  2. Check if the number is less than or equal to 1. If it is, it’s not a prime number. If not, proceed to the next step.
  3. Loop through 2 to the square root of the given number:
    1. If the number is divisible by any number in this range, it’s not a prime number.
    2. If it’s not divisible by any number in this range, it’s a prime number.

The reason we check divisibility up to the square root of the number is because a larger factor of the number would be a multiple of a smaller factor that has already been checked.

Code Implementation in C

Now let’s write the C code based on the logic we described:

Code Explanation

Here’s a detailed breakdown of the code above:

  1. #include<stdio.h> and #include<math.h>: These are preprocessor directives which tell the C compiler to include the stdio.h and math.h files before going to the actual compilation.
  2. int main(): The execution of the C program begins from the main() function.
  3. int num, i, flag = 0;: Here, we are declaring three integer variables num (to store the number entered by the user), i (to use in the loop), and flag (to check whether the number is prime or not).
  4. printf(“Enter a positive integer: “);: This function is used to print the message inside the double quotes.
  5. scanf(“%d”, &num);: This function is used to take input from the user. Here, %d is a format specifier. &num is the address of the variable num.
  6. for(i = 2; i <= sqrt(num); i++): This loop will run from 2 to the square root of the given number.
  7. if(num%i == 0): Checks if num is divisible by i. If it is, flag is set to 1 and the loop is broken.
  8. if (num <= 1) {flag = 1;}: Checks if the number is less than or equal to 1, in which case it is not prime.
  9. The last if…else structure is used to print whether the number is prime or not based on the value of flag.

Program Execution

If we execute this program and enter 29 when prompted for a positive integer, we would get the following output:

On the other hand, if we enter 4, we would get:

This C program accurately checks whether an entered number is prime. If you’re learning C or need to check for prime numbers in your code, this program offers a straightforward and effective way to do it.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top