Print prime numbers from 1 to N in reverse order

In this article, we will learn how to print prime numbers from 1 to N in reverse order. A prime number is a number greater than 1 that has no positive divisors other than 1 and itself. Our goal is to find all prime numbers in the given range from 1 to N and then print them in reverse order.

For example, given N = 20, the prime numbers between 1 and 20 are: 2, 3, 5, 7, 11, 13, 17, 19. The output should be the prime numbers printed in reverse order: 19, 17, 13, 11, 7, 5, 3, 2.

Syntax

bool isPrime(int num);
void printPrimesReverse(int N);

Method 1: Using Simple Loop and Prime Check

In this approach, we use a basic loop to check each number from N down to 1 and determine whether it's a prime number −

#include <stdio.h>
#include <stdbool.h>
#include <math.h>

bool isPrime(int num) {
    if (num <= 1) return false;
    for (int i = 2; i <= sqrt(num); ++i) {
        if (num % i == 0) return false;
    }
    return true;
}

int main() {
    int N = 20;
    printf("Prime numbers from 1 to %d in reverse order:<br>", N);
    
    for (int i = N; i >= 2; --i) {
        if (isPrime(i)) {
            printf("%d ", i);
        }
    }
    printf("<br>");
    return 0;
}
Prime numbers from 1 to 20 in reverse order:
19 17 13 11 7 5 3 2 

Method 2: Using Sieve of Eratosthenes

The Sieve of Eratosthenes is an efficient algorithm for finding all prime numbers up to a given number N. We first find all primes and then print them in reverse order −

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

void sieveReverse(int N) {
    bool *isPrime = (bool*)malloc((N + 1) * sizeof(bool));
    
    for (int i = 0; i <= N; i++) {
        isPrime[i] = true;
    }
    isPrime[0] = isPrime[1] = false;
    
    for (int i = 2; i * i <= N; ++i) {
        if (isPrime[i]) {
            for (int j = i * i; j <= N; j += i) {
                isPrime[j] = false;
            }
        }
    }
    
    for (int i = N; i >= 2; --i) {
        if (isPrime[i]) {
            printf("%d ", i);
        }
    }
    printf("<br>");
    free(isPrime);
}

int main() {
    int N = 30;
    printf("Prime numbers from 1 to %d in reverse order:<br>", N);
    sieveReverse(N);
    return 0;
}
Prime numbers from 1 to 30 in reverse order:
29 23 19 17 13 11 7 5 3 2 

Method 3: Store Primes and Print in Reverse

This method first finds all prime numbers up to N, stores them in an array, and then prints them in reverse order −

#include <stdio.h>
#include <stdbool.h>
#include <math.h>

bool isPrime(int num) {
    if (num <= 1) return false;
    for (int i = 2; i * i <= num; ++i) {
        if (num % i == 0) return false;
    }
    return true;
}

int main() {
    int N = 25;
    int primes[100];
    int count = 0;
    
    for (int i = 2; i <= N; ++i) {
        if (isPrime(i)) {
            primes[count++] = i;
        }
    }
    
    printf("Prime numbers from 1 to %d in reverse order:<br>", N);
    for (int i = count - 1; i >= 0; --i) {
        printf("%d ", primes[i]);
    }
    printf("<br>");
    return 0;
}
Prime numbers from 1 to 25 in reverse order:
23 19 17 13 11 7 5 3 2 

Complexity Comparison

Method Time Complexity Space Complexity Best For
Simple Loop O(N * sqrt(N)) O(1) Small N
Sieve of Eratosthenes O(N log log N) O(N) Large N
Store and Reverse O(N * sqrt(N)) O(?(N)) Multiple queries

Conclusion

We discussed three methods to print prime numbers from 1 to N in reverse order. The Sieve of Eratosthenes is most efficient for large N, while the simple loop approach works well for smaller inputs with minimal memory usage.

Updated on: 2026-03-15T11:05:15+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements