Prime Number Check Program in C

Prime Number Check Program in C

Here is a simple program in C that checks if a given number is prime:

#include <stdio.h>

int isPrime(int num) {
    if (num <= 1) {
        return 0;
    }

    for (int i = 2; i * i <= num; i++) {
        if (num % i == 0) {
            return 0;
        }
    }

    return 1;
}

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);

    if (isPrime(num)) {
        printf("%d is a prime number.\n", num);
    } else {
        printf("%d is not a prime number.\n", num);
    }

    return 0;
}

This program defines a function isPrime that takes an integer num as input and returns 1 if num is prime and 0 otherwise. The isPrime function uses a simple algorithm to check if num is divisible by any number from 2 to the square root of num. If it is divisible by any number in this range, it is not prime. Otherwise, it is prime.

In the main function, the program prompts the user to enter a number and then calls the isPrime function to check if the entered number is prime. The program then prints the result.

Please note that this program assumes the input is a positive integer. It does not handle negative numbers or non-integer inputs.

Let me know if you need further assistance!