c++ program to print natural numbers from 1 to 10 in reverse order using while loop
#include <iostream>
int main() {
int i = 10;
while (i >= 1) {
std::cout << i << " ";
i--;
}
return 0;
}
Explanation:
1. The #include <iostream>
directive is used to include the necessary library for input/output operations in C++.
The
main()
function is the entry point of the program.int i = 10;
initializes a variablei
with the value 10. This variable will be used to store the current number being printed.The
while (i >= 1)
loop is used to iterate through the numbers from 10 to 1. It will continue executing the loop as long as the conditioni >= 1
is true.std::cout << i << " ";
prints the value ofi
followed by a space.std::cout
is used to output text to the console.i--;
decrements the value ofi
by 1 after each iteration of the loop.After the loop finishes,
return 0;
is used to indicate that the program has successfully completed and returns 0 to the operating system.
The output of this program will be:
10 9 8 7 6 5 4 3 2 1