C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Loop Programs in C

Print Natural Numbers from N down to 1 (Reverse Order)

Introduction

Sometimes, you may need to print numbers in reverse order — from a given number N down to 1.
This can be done easily using a loop that decrements the counter variable after each iteration.

 

C Program: Print N Natural Numbers in Reverse Order

NOTE: The Program is written using for, while and do..while loop.
Method 1 is for loop, Method 2 is while loop and Method 3 is do..while loop

Method 1: Using for loop

C

#include <stdio.h>

 

int main() {

    int N, i;

 

    // Input

    printf("Enter the value of N: ");

    scanf("%d", &N);

 

    printf("Natural numbers from %d to 1 are:\n", N);

 

    // Loop from N down to 1

    for (i = N; i >= 1; i--) {

        printf("%d ", i);

    }

 

    printf("\n");

    return 0;

}

Output

 
OUTPUT 1 :
Enter the value of N: 10
Natural numbers from 10 to 1 are:
10 9 8 7 6 5 4 3 2 1


OUTPUT 2 :
Enter the value of N: 5
Natural numbers from 5 to 1 are:
5 4 3 2 1

Explanation

  1. User enters the upper limit N.
  2. The loop starts from N and decreases i by 1 each time (i--).
  3. The loop stops when i becomes less than 1.
  4. Each value is printed on the same line.

 

C Program: Print N Natural Numbers in Reverse Order

Method 2: Using while loop

In this program, we’ll print natural numbers in reverse order — from a user-defined number N down to 1.
A while loop is ideal for this because we can easily control the decrement condition.

C

#include <stdio.h>

 

int main() {

    int N;

 

    // Input

    printf("Enter the value of N: ");

    scanf("%d", &N);

 

    printf("Natural numbers from %d to 1 are:\n", N);

 

    // While loop (decrementing)

    while (N >= 1) {

        printf("%d ", N);

        N--;  // Decrement

    }

 

    printf("\n");

    return 0;

}

Output

 
OUTPUT 1 :
Enter the value of N: 8
Natural numbers from 8 to 1 are:
8 7 6 5 4 3 2 1


OUTPUT 2 :
Enter the value of N: 3
Natural numbers from 3 to 1 are:
3 2 1

Explanation

  1. User enters a number N.
  2. The loop condition N >= 1 ensures numbers are printed until N becomes 0.
  3. Inside the loop:
    • The current value of N is printed.
    • Then, N is decremented by 1 (N--).
  4. Loop stops when N becomes less than 1.

 

C Program: Print N Natural Numbers in Reverse Order

Method 3: Using do..while loop

A do-while loop is similar to a while loop, except it executes the loop body at least once, even if the condition is false.
This is useful when you want to ensure the statements inside the loop run at least one time.

A do-while loop ensures that the loop body runs at least once, even if the condition becomes false later.
We’ll use it here to print numbers in reverse order — from N down to 1.

 

C

#include <stdio.h>

 

int main() {

    int N;

 

    // Input

    printf("Enter the value of N: ");

    scanf("%d", &N);

 

    printf("Natural numbers from %d to 1 are:\n", N);

 

    // Do-while loop (decrementing)

    do {

        printf("%d ", N);

        N--;  // Decrement the number

    } while (N >= 1);

 

    printf("\n");

    return 0;

}

Output

 
OUTPUT 1 :
Enter the value of N: 6
Natural numbers from 6 to 1 are:
6 5 4 3 2 1


OUTPUT 2 :
Enter the value of N: 3
Natural numbers from 3 to 1 are:
3 2 1

Explanation

  1. The user inputs N.
  2. The do block executes first — prints the current number.
  3. Then, N is decremented by 1 (N--).
  4. The condition while (N >= 1) is checked — if true, it repeats.
  5. When N becomes less than 1, the loop stops.