C program to check Prime number

Check Number is Prime or Not

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

A prime number is a positive integer that is divisible only by 1 and number itself. For example: 2, 3, 5, 7, 11, 13, 17

Now the task is how to check whether a number is prime or composite, to check if a number is prime two conditions should be checked-
1) It should be a whole number greater than 1.
2) It should have only two factors i.e 1 and the number itself.

If these two conditions are true, then we can say that a number is a prime number.

#include <stdio.h>
#include<conio.h>
void main() {
    int num, j, flag = 0;
    clrscr();
printf("\n...Welcome To EduNews.Tech... ");
    printf("\n\nEnter a positive integer: ");
    scanf("%d", &num);
    for (j = 2; j <= num / 2; ++j) {
        // condition for non-prime
        if (num % j == 0) {
            flag = 1;
            break;
        }
    }
    if (num == 1) {
        printf("1 is neither prime nor composite.");
    }
    else {
        if (flag == 0)
            printf("%d is a prime number.", num);
        else
            printf("%d is not a prime number.", num);
printf("\n\n\n\n\t\t\tThankyou for Joining Us !");
    printf("\n\t\t\t!Regards EduNews !");
    }
    getch();
}

Program Output:

prime number output

I hope this post helps you to understand Prime number and its implementation in C programming language.

Keep coding 🙂

4 thoughts on “C program to check Prime number”

Leave a Reply

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