C Program to Check Prime Number By Creating a Function - Source Code, Output & Execution Flow

C Program to Check Prime Number By Creating a Function

This C program checks whether a number is prime by creating and calling a separate function.

#include <stdio.h>
int isPrime(int n) {
    int i;
    if (n <= 1) return 0;
    for (i = 2; i * i <= n; i++) {
        if (n % i == 0) return 0;
    }
    return 1;
}
int main() {
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);
    if (isPrime(n)) printf("%d is prime.
", n);
    else printf("%d is not prime.
", n);
    return 0;
}
Enter a number: 13
13 is prime.

In this program, the input is read first when required, the core logic is applied step by step, and the final result is displayed using printf(). Follow the execution flow beside the code to understand how each statement contributes to the output.

Sai Kishore Randhi, author
About the author

Sai Kishore Randhi

Embedded systems, VLSI, and chip design

Sai Kishore writes about embedded systems, VLSI, chip design, and the programming concepts behind modern hardware.

View LinkedIn profile ↗