In following C programs, we will print natural numbers in reverse order from N to 1 using for loop, while loop and do-while loop.
Required Knowledge
C program to print natural numbers from N to 1 in reverse order using for loop
#include <stdio.h> int main() { int counter, N; printf("Enter a Positive Number\n"); scanf("%d", &N); printf("Printing Numbers form %d to 1\n", N); for(counter = N; counter > 0; counter--) { printf("%d \n", counter); } return 0; }
Output
Enter a Positive Number 10 Printing Numbers form 10 to 1 10 9 8 7 6 5 4 3 2 1
C program to print numbers in reverse order from 10 to 1 using while loop
#include <stdio.h> int main() { int counter, N; printf("Enter a Positive Number\n"); scanf("%d", &N); printf("Printing Numbers form %d to 1\n", N); counter = N; while(counter > 0) { printf("%d \n", counter); counter--; } return 0; }
Output
Enter a Positive Number 6 Printing Numbers form 6 to 1 6 5 4 3 2 1
C program to print numbers in reverse from N to 1 using do-while loop
#include <stdio.h> int main() { int counter, N; printf("Enter a Positive Number\n"); scanf("%d", &N); printf("Printing Numbers form %d to 1\n", N); counter = N; do { printf("%d \n", counter); counter--; } while(counter > 0); return 0; }
Output
Enter a Positive Number 6 Printing Numbers form 6 to 1 6 5 4 3 2 1
Related Topics