Check for Prime Number in C

This program checks if a given number is prime.

#include <stdio.h>

#include <stdbool.h>

 int main() {
    int n, i;
    bool isPrime = true;
 
    printf("Enter a positive integer: ");
    scanf("%d", &n);
 
    if (n == 0 || n == 1) {
        isPrime = false;
    } else {
        for (i = 2; i <= n / 2; ++i) {
            if (n % i == 0) {
                isPrime = false;
                break;
            }
        }
    }
 
    if (isPrime)
        printf("%d is a prime number.\n", n);
    else
        printf("%d is not a prime number.\n", n);
 
    return 0;
}

No comments:

Post a Comment

MS Excel Logical Functions

Logical functions in Excel are powerful tools that help you make decisions based on conditions. Whether you're comparing values or testi...

Post Count

Loading...