C Program To Check Whether a Number is Prime or Not

In this post, we will learn how to check whether a number is prime or not using C Programming language.

C Program To Check Whether a Number is Prime or Not

A number is called a Prime number, if it is divisible only by itself and one. This means a Prime number has only two factors – 1 and the number itself. For example: 2, 3, 5, 7, 11, . . . etc.

A number is called a Composite Number, if it has more than two factors. For example: 4, 15, 26, 98, . . . etc.

Note: 1 is neither a prime number nor composite number.

So, without further ado, let’s begin the tutorial.

C Program To Check Whether a Number is Prime or Not

// C Program To Check Whether a Number is Prime or Not
#include <stdio.h>
int main(){
    int num, i, c = 0;
    
    // Asking for Input
    printf("Enter an Number: ");
    scanf("%d", &num);
    
    // logic
    for (i = 1; i <= num; i++){
        if (num % i == 0){
            c++;
        }
    }
    if (c == 2){
        printf("%d is a Prime Number.", num);
    }
    else {
        printf("%d is not a Prime Number.", num);
    }
    return 0;
}

Output 1

Enter an Number: 7
7 is a Prime Number.

Output 2

Enter an Number: 57
57 is not a Prime Number.

How Does This Program Work ?

    int num, i, c = 0;

In this program, we have declared three int data type variables named as num, i and c. Variable c is assigned a value of 0.

    // Asking for Input
    printf("Enter an Number: ");
    scanf("%d", &num);

Then, the user is asked to enter a number which he/she wants to check. This number will get stored in the num named variable.

    // logic
    for (i = 1; i <= num; i++){
        if (num % i == 0){
            c++;
        }
    }

Now, this program checks for the number of integers, num is completely divisible with. And the value of the number of factors is stored in the c named variable.

    if (c == 2){
        printf("%d is a Prime Number.", num);
    }
    else {
        printf("%d is not a Prime Number.", num);
    }

If the num contains exactly 2 factors, then the given number is a Prime number otherwise it’s not a Prime number.

Some of the used terms are as follow:

#include <stdio.h> – In the first line we have used #include, it is a preprocessor command that tells the compiler to include the contents of the stdio.h(standard input and output) file in the program. 

The stdio.h is a file which contains input and output functions like scanf() and printf() to take input and display output respectively. 

Int main() – Here main() is the function name and int is the return type of this function. The Execution of any Programming written in C language begins with main() function. 

scanf() – scanf() function is used to take input from the user.  

printf() – printf() function is used to display and print the string under the quotation to the screen.  

for loop – A loop is used for initializing a block of statements repeatedly until a given condition returns false.

If. . . else – An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. If Statement executes when the Boolean expression is True. 

% – It is known as Modulus Operator and provides remainder after division.

// – Used for Commenting in C. 

Conclusion

I hope after going through this post, you understand how to check whether a number is prime or not using C Programming language.

If you have any doubt regarding the topic, feel free to contact us in the comment section. We will be delighted to help you.

1 thought on “C Program To Check Whether a Number is Prime or Not”

Leave a Comment

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