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:
The
#include <iostream>
directive includes the input/output stream library, which gives us access to thestd::cout
andstd::cin
objects for displaying output and getting input respectively.The
int main()
function is the entry point of the program. It is where the program execution begins.The
int number;
line declares an integer variable namednumber
to store the input from the user.The
std::cout << "Enter a number: ";
statement uses thestd::cout
object to display the message "Enter a number: " to the user.The
std::cin >> number;
statement uses thestd::cin
object to read the user input and assigns it to thenumber
variable.The
std::cout << "You entered: " << number << std::endl;
statement uses thestd::cout
object to display the message "You entered: " followed by the value of thenumber
variable, and then inserts an endline character (std::endl
) to move the cursor to the next line.The
return 0;
statement terminates themain()
function and indicates that the program has executed successfully. The value0
is typically used to indicate successful program execution.