Skip to content
Laviyx Learn

Try

PPS Lab · Experiment 13

Prime numbers from 1 to n

CExpression evaluationJNTUH R22 · CS108ES

Aim

To write a C program that reads a number n and prints all the prime numbers between 1 and n.

Algorithm

  1. Read n. If n is less than 1, print a message and stop.
  2. For each number num from 2 to n:
    • Assume num is prime.
    • For each i from 2 while i × i ≤ num: if num % i is 0, num is not prime, so stop checking.
    • If num is still prime, print it and add 1 to the count.
  3. If the count is 0, print that there are no primes in the range; otherwise print the count.

Program

C
#include <stdio.h>

/* Returns 1 if num is prime, otherwise 0 */
int is_prime(int num) {
    if (num < 2) {
        return 0;
    }
    for (int i = 2; i * i <= num; i++) {
        if (num % i == 0) {
            return 0;
        }
    }
    return 1;
}

int main(void) {
    int n;
    printf("Enter the value of n: ");
    if (scanf("%d", &n) != 1) {
        printf("Invalid input\n");
        return 1;
    }
    if (n < 1) {
        printf("n must be at least 1\n");
        return 1;
    }

    int count = 0;
    printf("Prime numbers between 1 and %d:\n", n);
    for (int num = 2; num <= n; num++) {
        if (is_prime(num)) {
            printf("%d ", num);
            count++;
        }
    }

    if (count == 0) {
        printf("None\n");
    } else {
        printf("\nTotal: %d prime numbers\n", count);
    }
    return 0;
}

Output

n = 50

Output 1
Enter the value of n: 50
Prime numbers between 1 and 50:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 
Total: 15 prime numbers

n = 1 (no primes)

Output 2
Enter the value of n: 1
Prime numbers between 1 and 1:
None

Recorded by compiling and running this exact program. Typed input appears after each prompt.

How it works

The prime test is written as a separate function is_prime, so main only has to loop over the numbers and print the ones that pass. return 0 inside the loop ends the function as soon as a divisor is found.

1 is not a prime number, so the outer loop starts at 2. There are 15 primes up to 50.

Common mistakes

  • Starting the outer loop at 1 and printing 1 as a prime. A prime must be greater than 1.

  • Using the same variable name for the outer and inner loops. The inner loop then changes the outer counter and numbers are skipped or the loop never ends. A separate function avoids this.

  • Forgetting to reset the prime flag for each new number when the check is written inside main. Once one number is found not prime, every later number is reported as not prime too.

  • Checking divisors from 2 to num instead of num − 1. Every number divides itself, so nothing would be prime.

Viva questions

Try to answer each one aloud before you open it.

  • Why is the prime check written as a separate function?

    It keeps main short and easy to read, the check can be tested on its own, and it can be reused in other programs. Returning from the function also ends the check as soon as a divisor is found.

  • How many prime numbers are there between 1 and 50?

    15: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43 and 47.

  • Why does the inner loop stop when i × i is greater than num?

    If num had a divisor larger than its square root, it would also have a matching divisor smaller than the square root, which the loop would already have found.

  • What is the Sieve of Eratosthenes?

    Another way to find all primes up to n. Write all numbers from 2 to n, then repeatedly take the next number not crossed out and cross out all its multiples. The numbers left are prime. It is faster when n is large, but needs an array.

Learn the concepts

All Programming for Problem Solving Lab experiments →