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++
// C++ program to take an integer
// as input and print it
#include <iostream>
using namespace std;
// Driver code
int main()
{
// Declare the variables
int num;
// Input the integer
cout << "Enter the integer: ";
cin >> num;
// Display the integer
cout << "Entered integer is: " << num;
return 0;
}
Output
Enter the integer: 10
Entered integer is: 10
Similar Reads
How to Take Input in Array in C++? Arrays in C++ are derived data types that can contain multiple elements of the same data type. They are generally used when we want to store multiple elements of a particular data type under the same name. We can access different array elements using their index as they are stored sequentially in th
3 min read
Integer literal in C/C++ (Prefixes and Suffixes) Integer literal is a type of literal for an integer whose value is directly represented in source code. For example, in the assignment statement x = 1, the string 1 is an integer literal indicating the value 1, while in the statement x = 0x10 the string 0x10 is an integer literal indicating the valu
4 min read
A creative C++ program to Zoom digits of an integer Write a C (or C++) program to ZOOM (magnify) the digits of an integer. It should take an integer from the user and display each digit of the integer in magnified form using some pattern. Examples: Input : 123 Output : @ @@ @ @ @@@@@ ------------------------------- @@@@ @ @ @ @ @@@@ -----------------
4 min read
Reading Lines by Lines From a File to a Vector in C++ STL Prerequisites: STL in C++Vector in C++File handling in C++ The Standard Template Library (STL) is a set of C++ template classes to provide common programming data structures and functions such as lists, stacks, arrays, etc. It is a library of container classes, algorithms, and iterators. Vector in C
2 min read
Representation of Int Variable in Memory in C++ To understand the Representation of int variable in memory in C/C++ below is the basic program to create a variable and its size. Usually, the value of an int variable is stored in a series of bytes that are represented as the variable's representation as memory. Depending on the hardware and compil
5 min read
C++ Program to Convert String to Integer Given a string of digits, the task is to convert the string to an integer. Examples: Input : str = "12345" Output : 12345 Input : str = "876538"; Output : 876538 Input : str = "0028"; Output : 28 CPP // C++ program to convert String into Integer #include <bits/stdc++.h> using namespace std; //
1 min read