- Write a C program to print natural numbers in reverse order from N to 1 using for loop.
- Wap in C to print numbers in reverse order using while and do-while loop
Required Knowledge
- C printf and scanf functions
- For loop in C
- While loop in C
- Do while loop in C
C program to print natural numbers from N to 1 in reverse order using for loop
#include <stdio.h> int main() { int counter, N; /* * Take a positive number as input form user */ printf("Enter a Positive Number\n"); scanf("%d", &N); printf("Printing Numbers form %d to 1\n", N); /* * Initialize the value of counter to N and keep on * decrementing it's value in every iteration till * counter > 0 */ 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; /* * Take a positive number as input form user */ printf("Enter a Positive Number\n"); scanf("%d", &N); printf("Printing Numbers form %d to 1\n", N); /* * Initialize the value of counter to N and keep on * decrementing it's value in every iteration till * counter > 0 */ 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; /* * Take a positive number as input form user */ printf("Enter a Positive Number\n"); scanf("%d", &N); printf("Printing Numbers form %d to 1\n", N); /* * Initialize the value of counter to N and keep on * decrementing it's value in every iteration till * counter > 0 */ 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