# C Program to Check Whether a Number is Prime or Not

In this programming tutorial, we will create a C program to determine whether a given number is prime or not. The program will include a detailed explanation of the algorithm used, the C code implementation, and examples demonstrating the output.

## Algorithm for Checking Prime Numbers

1. Input: Accept a number (`num`) from the user.
2. Special Cases: Check if `num` is less than 2. If true, the number is not prime.
3. Loop: Iterate from 2 to the square root of `num`.
• If `num` is divisible evenly by any number in the range, it is not prime.
4. Output: Display whether the number is prime or not.

## C Program for Checking Prime Numbers

``````#include <stdio.h>
#include <math.h>

// Function to check whether a number is prime
int isPrime(int num) {
if (num < 2) {
return 0; // Not prime
}

for (int i = 2; i <= sqrt(num); i++) {
if (num % i == 0) {
return 0; // Not prime
}
}

return 1; // Prime
}

int main() {
int num;

// Input a number from the user
printf("Enter a number: ");
scanf("%d", &num);

// Check whether the number is prime using the isPrime function
if (isPrime(num)) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}

return 0;
}
``````

## Output Example

### Example 1: Check if 7 is a Prime Number

``````Enter a number: 7
7 is a prime number.
``````

### Example 2: Check if 10 is a Prime Number

``````Enter a number: 10
10 is not a prime number.
``````

## Explanation

1. Input: The user provides a number (e.g., 7).
2. Function Call: The `isPrime` function is called with the provided number.
3. Special Cases: The function checks if the number is less than 2 and returns 0 if true (not prime).
4. Loop: For numbers greater than or equal to 2, the function checks for factors up to the square root of the number.
5. Output: The program displays whether the input number is prime or not.

## Conclusion

This C program efficiently checks whether a given number is prime or not using a simple algorithm. The `isPrime` function handles the prime-checking logic, and the main function takes user input to apply the check. Feel free to test the program with different numbers to observe the results. If you have any questions or need further clarification, please don’t hesitate to ask!