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. Logic to print natural numbers in reverse for 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, Relational operators, For loop
Logic to print natural numbers in reverse
Logic to print natural numbers in reverse is almost similar to printing natural numbers from 1 to n.
Step by step descriptive logic to print natural numbers in reverse.
- Input start limit from user. Store it in some variable say start.
- Run a loop from start to 1 and decrement 1 in each iteration. The loop structure should look like
for(i=start; i>=1; i--)
.Let me first answer few question popping in your head right now.
Why we need to initialize loop from start? Because the first value we need to print is start.
Why use
i>=1
why noti<=1
? Because the loop we are constructing is in decrementing order with numbers to print must be greater than or equal to 1.Why decrement the value of i instead of increment? Because we are running downwards (reverse), from start to 1.
- 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' to 1 and
* decrement 1 in each iteration
*/
for(i=start; i>=1; i--)
{
printf("%d\n", i);
}
return 0;
}
Learn more – Program to print natural numbers in reverse using while loop.
Logic to print natural number in reverse in given range
If you got above logic, then you can easily modify to print natural numbers in reverse in given range. We need to make two modification. First with loop initialization and next with loop condition.
Step by step descriptive logic to print natural numbers in reverse in given range.
- Input start limit to print from user. Store it in some variable say start.
- Input end limit to print from user. Store it in some variable say end.
- Run a loop from start to end, decrement the loop by 1 in each iteration. 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 limit from user */
printf("Enter starting value: ");
scanf("%d", &start);
printf("Enter end value: ");
scanf("%d", &end);
/*
* Run loop from 'start' to 'end' and
* decrement by 1 in each iteration
*/
for(i=start; i>=end; i--)
{
printf("%d\n", i);
}
return 0;
}
Output
Enter starting value: 15 Enter end value: 5 15 14 13 12 11 10 9 8 7 6 5
Happy coding 😉
Recommended posts
- Loop programming exercises index.
- C program to print all natural numbers from 1 to n.
- C program to print all alphabets from a to z.
- C program to print all even numbers from 1 to n.
- C program to print all odd numbers from 1 to n.
- C program to find sum of natural numbers in given range.
- C program to find sum of even numbers in given range.