How to Read and Print an Integer value in C++
Last Updated :
03 Jul, 2023
The given task is to take an integer as input from the user and print that integer in C++ language. In this article, we will learn how to read and print an integer value.
In the below program, the syntax and procedures to take the integer as input from the user is shown in C++ language.
Standard Input Stream
- The user enters an integer value when asked.
- This value is taken from the user with the help of the cin method. The cin method, in C++, reads the value from the console into the specified variable.
Syntax:
cin >> variableOfXType;
Here,
>> is the extraction operator and is used along with the object cin for reading inputs.
The extraction operator extracts the data from the object cin which is entered using the keyboard.
For an integer value, the X is replaced with the type int. The syntax of the cin method becomes as follows then:
cin >> variableOfIntType;
This entered value is now stored in the variableOfIntType. Now to print this value, cout method is used.
Standard Output Stream
The cout method, in C++, prints the value passed as the parameter to it, on the console screen.
Syntax:
cout << variableOfXType;
Here,
<< is the insertion operator.
The data needed to be displayed on the screen is inserted in the standard output stream (cout) using the insertion operator (<<).
For an integer value, the X is replaced with the type int. The syntax of cout() method becomes as follows then:
cout << variableOfIntType;
Hence, the integer value is successfully read and printed.
Below is the C++ program to read and print an integer value:
C++
#include <iostream>
using namespace std;
int main()
{
int num;
cout << "Enter the integer: " ;
cin >> num;
cout << "Entered integer is: " << num;
return 0;
}
|
Output
Enter the integer: 10
Entered integer is: 10
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...