C Program to check if a number is prime or not

Category: C ProgramTags: cprogram function

C Program to check if a number is prime or not using function

C Program to check if a number is prime or not using function

#include <stdio.h>

// function declaration
int isPrime(int);

void main()
{
    int num;

    printf("Enter the number: ");
    scanf("%d", &num);

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

// function definition
int isPrime(int num)
{
    // return 1 if number is prime else 0
    int i;
    for (i = 2; i <= num / 2; i++)
    {
        if (num % i == 0)
            return 0;
    }
    return 1;
}

Output

Output

Enter the number: 7
7 is a prime number