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

Previous Program Next Program

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 how to print natural numbers from 1 to n. In case you missed that, check it here.

Now, let us talk about the logic of this program. Logic of this is almost similar to the one we did in previous program (print natural 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 N.
  2. Initialize a loop from N goes till 1, decrementing 1 in each loop. The loop structure should look like - for(i=N; i>=1; i--).

    Why I have initialized the loop from N? Because starting value we need to print is N.
    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 N to 1.

  3. Inside the loop body, print the value of i.

Let us write the code now.

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, n;

    /*
     * Read a number from user
     */
    printf("Enter value of n: ");
    scanf("%d", &n);

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

    return 0;
} 
Output
Enter value of n: 10
10
9
8
7
6
5
4
3
2
1

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. Read the starting value to print. Store it in some variable say start.
  2. Read the ending value to print. Store it in some variable say end.
  3. Initialize a loop from start, that runs upto 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;

    /*
     * Read 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 approaches -

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

Happy coding ;)

You may also like

Previous Program Next Program
Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)

No comments:

Post a Comment