Write a C++ Program to Print natural numbers in reverse order from given value to 1. This C++ program allows you to enter the maximum integer number from where the printing starts. Next, we used the for loop (for(int i = number; i >= 1; i–)) to iterate numbers from that number to 1 by decrementing the i value. Within the loop, we print the i value.
#include<iostream> using namespace std; int main() { int number; cout << "\nPlease Enter Maximum Value to print Natural Numbers = "; cin >> number; cout << "\nList of Natural Numbers from " << number << " to 1 are\n"; for(int i = number; i >= 1; i--) { cout << i <<" "; } return 0; }
Please Enter Maximum Value to print Natural Numbers = 10
List of Natural Numbers from 10 to 1 are
10 9 8 7 6 5 4 3 2 1
C++ program to Print Natural Numbers in Reverse using While Loop
#include<iostream> using namespace std; int main() { int number; cout << "\nPlease Enter Maximum Value to print Natural Numbers = "; cin >> number; int i = number; cout << "\nList of Natural Numbers from " << number << " to 1 are\n"; while(i >= 1) { cout << i <<" "; i--; } return 0; }
Please Enter Maximum Value to print Natural Numbers = 25
List of Natural Numbers from 25 to 1 are
25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
This C++ print natural numbers in reverse example allow entering a minimum and maximum value. Next, it prints natural numbers between maximum and minimum.
#include<iostream> using namespace std; int main() { int minNat, maxNat; cout << "\nPlease Enter Minimum to print Natural Numbers = "; cin >> minNat; cout << "\nPlease Enter Maximum to print Natural Numbers = "; cin >> maxNat; cout << "\nList of Natural Numbers from " << maxNat << " to " << minNat << " are\n"; for(int i = maxNat; i >= minNat; i--) { cout << i <<" "; } return 0; }