Prime Checker Library

Prime Checker Library

Easy C Functions 39 views
Explanation Complexity

Problem Statement

Create a function isPrime() to check the number. Create another function printPrimesInRange() to print all the primes in the range using isPrime(). Example of modular code.

Input Format

Two integers:

•start

• end

Output Format

All prime numbers between start and end.

Example

start = 10
end = 30


11 13 17 19 23 29

Constraints

• start ≥ 1

• end ≥ start

Concept Explanation

The task is divided into two functions to keep the code modular and reusable.

Step-by-Step Explanation

isPrime(number)

1.If number ≤ 1, return false.

2.Loop from 2 to number − 1.

3.If number is divisible by any value in the loop:

• Return false.

4.If no divisor is found:

• Return true.

printPrimesInRange(start, end)

1.Loop from start to end.

2.For each number, call isPrime(number).

3.If isPrime() returns true:

• Print the number.

4.Continue until the range is completed.

Concept Explanation

The task is divided into two functions to keep the code modular and reusable.

Step-by-Step Explanation

isPrime(number)

1.If number ≤ 1, return false.

2.Loop from 2 to number − 1.

3.If number is divisible by any value in the loop:

• Return false.

4.If no divisor is found:

• Return true.

printPrimesInRange(start, end)

1.Loop from start to end.

2.For each number, call isPrime(number).

3.If isPrime() returns true:

• Print the number.

4.Continue until the range is completed.

Input / Output Format

Input Format
Two integers:

•start

• end
Output Format
All prime numbers between start and end.
Constraints
• start ≥ 1

• end ≥ start

Examples

Input:
start = 10 end = 30
Output:
11 13 17 19 23 29

Example Solution (Public)

C
#include <stdio.h>

/* Function to check if a number is prime */
int isPrime(int num) {
    int i;

    if (num <= 1)
        return 0;

    for (i = 2; i * i <= num; i++) {
        if (num % i == 0)
            return 0;
    }

    return 1;
}

/* Function to print primes in a given range */
void printPrimesInRange(int start, int end) {
    int i;

    printf("Prime numbers between %d and %d:n", start, end);

    for (i = start; i <= end; i++) {
        if (isPrime(i)) {
            printf("%d ", i);
        }
    }
    printf("n");
}

int main() {
    int start, end;

    printf("Enter start of range: ");
    scanf("%d", &start);

    printf("Enter end of range: ");
    scanf("%d", &end);

    printPrimesInRange(start, end);

    return 0;
}

Official Solution Code

#include <stdio.h>

/* Function to check if a number is prime */
int isPrime(int num) {
    int i;

    if (num <= 1)
        return 0;

    for (i = 2; i * i <= num; i++) {
        if (num % i == 0)
            return 0;
    }

    return 1;
}

/* Function to print primes in a given range */
void printPrimesInRange(int start, int end) {
    int i;

    printf("Prime numbers between %d and %d:n", start, end);

    for (i = start; i <= end; i++) {
        if (isPrime(i)) {
            printf("%d ", i);
        }
    }
    printf("n");
}

int main() {
    int start, end;

    printf("Enter start of range: ");
    scanf("%d", &start);

    printf("Enter end of range: ");
    scanf("%d", &end);

    printPrimesInRange(start, end);

    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.