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.
Input N: 10
Natural numbers from 10-1 in reverse: 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
Required knowledge
- 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--)
. - 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;
}
Logic to print natural number 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;
}
Comments
Post a Comment