C Program to check if a number is prime or not in a given interval

Category: C ProgramTags: cprogram function

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

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

#include <stdio.h>

// function declaration
int isPrime(int);

void main()
{
    int start, end, num;

    printf("Enter the number to start: ");
    scanf("%d", &start);
    printf("Enter the number to end: ");
    scanf("%d", &end);

    printf("\nPrime numbers between %d to %d are-\n", start, end);
    for (num = start; num <= end; num++)
    {
        if (isPrime(num))
            printf("%d\n", 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 to start: 1
Enter the number to end: 20

Prime numbers between 1 to 20 are-
1
2
3
5
7
11
13
17
19