c++ program to take input from user

#include <iostream>

int main() {
    int number;

    std::cout << "Enter a number: ";
    std::cin >> number;

    std::cout << "You entered: " << number << std::endl;

    return 0;
}

Explanation:

  1. The #include <iostream> directive includes the input/output stream library, which gives us access to the std::cout and std::cin objects for displaying output and getting input respectively.

  2. The int main() function is the entry point of the program. It is where the program execution begins.

  3. The int number; line declares an integer variable named number to store the input from the user.

  4. The std::cout << "Enter a number: "; statement uses the std::cout object to display the message "Enter a number: " to the user.

  5. The std::cin >> number; statement uses the std::cin object to read the user input and assigns it to the number variable.

  6. The std::cout << "You entered: " << number << std::endl; statement uses the std::cout object to display the message "You entered: " followed by the value of the number variable, and then inserts an endline character (std::endl) to move the cursor to the next line.

  7. The return 0; statement terminates the main() function and indicates that the program has executed successfully. The value 0 is typically used to indicate successful program execution.

Let me know if you need further assistance!