C++ Program to print number entered by user

In this program, the user is asked to enter any integer type value.  This value is taken from the user with the help of certain methods in the programming language. This entered value is then stored in a variable and the value of the variable is printed on the screen with the help of various programming methods. 

How to Print Number Entered by User?

As in other languages, C++  also provides functions to read data from the user using the terminal. We will display some messages to the user to input data, the message should be related to the data we need and he will enter data from the keyboard. The user input will be stored in a variable and print that variable value.

How to read an integer and print that number in C++?

Here we are explaining how to write a C++ program to take input from the user and display that values. 

In programing, We are not allowed to have variables, functions, etc with the same name. So to avoid such conflicts we use namespaces. So for one namespace, we can have one unique name and the same name can also be used in other namespaces. Here we are using namespace std to write cout, cin, etc instead of std::cin, std::cout. The code will more readable and clearer to the beginner.

The program always starts with the main function. Within the main body declare an integer type variable to store the integer.


cout << "Enter an integer: ";
 

Using the object cout of class iostream pass a message to "enter any integer" to the screen.


cin >> number;
 

Next, Read the entered integer and store the value within the int type variable early declared with the help of the object cin of class iostream. Cin is used to accept inputs from the standard input device.


 cout << "You entered " << number;  
 

Print the value stored in the integer variable on the screen using cout and exit the program. 

ALGORITHM

Step 1:  Call the header file iostream.

Step 2: Use the namespace std.

Step 3: Open the integer type main function; int main().

Step 4: Declare an integer type variable; number.

Step 5: Print a message on the screen to “Enter an integer:” using cout.

Step 6: Read the entered integer to the variable ‘number’ using cin.

Step 7: Display the value of the variable ‘number ‘ on the screen using cout.

Step 8: Exit.

C++ Source Code

                                          #include <iostream>
using namespace std;

int main() {    
    int number;

    cout << "Enter an integer: ";
    cin >> number;

    cout << "You entered " << number;    
    return 0;
}
                                      

OUTPUT

Enter an integer: 100
You entered 100