C Program to Print Natural Numbers from 1 to N In Reverse Order using for loop

Lets write a simple C program to print natural numbers from 1 to N in reverse order, using for loop.

Related Read:
For Loop In C Programming Language
C Program to Print Natural Numbers from 1 to N using for loop

C Program to Print Natural Numbers from 1 to N In Reverse Order using for loop



YouTube Link: https://www.youtube.com/watch?v=07rwVW3TVUo [Watch the Video In Full Screen.]


Source Code: C Program to Print Natural Numbers from 1 to N In Reverse Order using for loop

 
#include<stdio.h>

int main()
{
    int num, count;

    printf("Enter a positive number\n");
    scanf("%d", &num);

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

    for(count = num; count >= 1; count--)
    {
        printf("%d\n", count);
    }

    return 0;
}

Output:
Enter a positive number
14

Natural numbers from 14 to 1 are:
14
13
12
11
10
9
8
7
6
5
4
3
2
1

Natural numbers from 10 to 1 are:

Logic To Print Natural Numbers from 1 to N In Reverse Order using for loop

We ask the user to enter a positive number, and store it inside a variable num. We assign value of num to variable count. We iterate through the loop until the count is equal to zero. For example, if user enters num = 5, we iterate the loop 5 times. In for loops modification section we keep decrementing the value of variable count. Once the value of count becomes 0, we exit the for loop. For each iteration we print the value of count.

This way we printout the natural numbers from 1 to N, in reverse order.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert