Lets write a simple C program to print natural numbers from 1 to N in reverse order, using while loop.
Related Read:
while loop in C programming
C Program to Print Natural Numbers from 1 to N using While loop
Source Code: C Program to Print Natural Numbers from 1 to N In Reverse Order using While loop
#include < stdio.h > int main() { int num; printf("Enter a positive number\n"); scanf("%d", &num); printf("\nNatural numbers from 1 to %d are:\n", num); while(num) { printf("%d ", num); num--; } printf("\n"); return 0; }
Output 1:
Enter a positive number
10
Natural numbers from 1 to 10 are:
10 9 8 7 6 5 4 3 2 1
Output 2:
Enter a positive number
14
Natural numbers from 1 to 14 are:
14 13 12 11 10 9 8 7 6 5 4 3 2 1
C Program to Print Natural Numbers from 1 to N In Reverse Order using While loop
[youtube https://www.youtube.com/watch?v=RDh7zjoLYNg]
Logic To Print Natural Numbers from 1 to N In Reverse Order using While loop
We ask the user to enter a positive number, and store it inside a variable num. We iterate through the loop until the number is equal to zero. For example, if user enters num = 5, we iterate the loop 5 times. Inside the while loop we keep decrementing the value of variable num. Once the value of num becomes 0, we exit the loop. For each iteration we print the value of num.
This way we printout all 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