How to Read and Print an Integer value in C++

The given task is to take an integer as input from the user and print print that integer in C++ language.

In below program, the syntax and procedures to take the integer as input from the user is shown in C++ language.

Steps:

  1. The user enters an integer value when asked.
  2. This value is taken from the user with the help of cin method. The cin method, in C++, reads the value from the console into the specified variable.

    Syntax:

    cin >> variableOfXType;
    
    where >> 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.
    
  3. For an integer value, the X is replaced with type int. The syntax of cin method becomes as follows then:

    Syntax:

    cin >> variableOfIntType;
    
  4. This entered value is now stored in the variableOfIntType.
  5. Now to print this value, cout method is used. The cout method, in C++, prints the value passed as the parameter to it, on the console screen.

    Syntax:

    cout << variableOfXType;
    
    where << 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 (<<).
    
  6. For an integer value, the X is replaced with type int. The syntax of cout() method becomes as follows then:

    Syntax:

    cout << variableOfIntType;
    
  7. Hence, the integer value is successfully read and printed.

Program:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to take an integer
// as input and print it
  
#include <iostream>
using namespace std;
  
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;
}

chevron_right


Output:

Enter the integer: 10
Entered integer is: 10


My Personal Notes arrow_drop_up

Technical Content Engineer at GeeksForGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.