C program to print natural numbers in reverse from n to 1

Write a C program to print all natural numbers in reverse from n to 1 using for loop. How to print natural numbers in reverse order in C programming. Printing natural numbers from n to 1 in C programming. Logic to print reverse natural numbers in a given range in C program.

Example

Input

Input N: 10

Output

Natural numbers from 10-1 in reverse: 
10, 9, 8, 7, 6, 5, 4, 3, 2, 1

Required knowledge

Basic C programming, For loop

Logic to print natural numbers in reverse

We already talked about loop in the previous exercise. I explained to print natural numbers from 1 to n.

Read more - Program to print natural numbers in range

Let us talk about the logic of this program. Logic of this is almost similar to printing numbers from 1 to n. Below is the step by step descriptive logic to print natural numbers in reverse.

  1. Read the starting value to print from user. Store it in some variable say start.
  2. Initialize a loop from start goes till 1, decrementing 1 in each loop. The loop structure should look like for(i=start; i>=1; i--). Here you might think, Why I have initialized the loop from start? Because the starting value we need to print is start.
    Why I have used i>=1? Because all values to be printed must be greater than or equal to 1.
    Why I have decremented the value of i rather incrementing? Because we are going downwards, from start to 1.
  3. Inside the loop body, print the value of i.

Program to print natural numbers in reverse

/**
 * C program to all natural numbers in reverse from n to 1
 */

#include <stdio.h>

int main()
{
    int i, start;

    /*
     * Input start range from user
     */
    printf("Enter starting value: ");
    scanf("%d", &start);

    /*
     * Run loop from start and go till 1, 
     * Decrementing by 1
     */ 
    for(i=start; i>=1; i--)
    {
        printf("%d\n", i);
    }

    return 0;
}

Logic to print natural number in reverse in given range

If you got the above logic, then it will take small modifications to derive the logic for range values. We need to change in loop initialization and condition part. Logic to print natural numbers from x to y in reverse.

  1. Input starting value to print. Store it in some variable say start.
  2. Input ending value to print. Store it in some variable say end.
  3. Initialize a loop from start, that goes till end, decrementing the loop by 1. The loop structure should look like for(i=start; i>=end; i--).

Program to print natural number in reverse in given range

/**
 * C program to all natural numbers in reverse in given range
 */

#include <stdio.h>

int main()
{
    int i, start, end;

    /*
     * Input start and end value from user
     */
    printf("Enter starting value: ");
    scanf("%d", &start);
    printf("Enter end value: ");
    scanf("%d", &end);

    /*
     * Run loop from start and go till end, 
     * Decrementing by 1
     */ 
    for(i=start; i>=end; i--)
    {
        printf("%d\n", i);
    }

    return 0;
}

Enhance your coding skills by learning this using other approach.

Read more - Program to print natural numbers in reverse using while loop

Output

Enter starting value: 15
Enter end value: 5
15
14
13
12
11
10
9
8
7
6
5

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>