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++.

  1. The main() function is the entry point of the program.

  2. int i = 10; initializes a variable i with the value 10. This variable will be used to store the current number being printed.

  3. 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 condition i >= 1 is true.

  4. std::cout << i << " "; prints the value of i followed by a space. std::cout is used to output text to the console.

  5. i--; decrements the value of i by 1 after each iteration of the loop.

  6. 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