Here is a C++ program for input and output of integers. In this program, we will learn about taking an integer input from user using cin and then printing an integer on screen using cout. To perform any input and output operation in a C++ program, we have to include iostream header in program. Here is a C++ program for input and output of integer.
C++ Program to take integer input from user and print it
#include <iostream> using namespace std; int main() { int inputNumber; cout << "Enter an integer\n"; cin >> inputNumber; cout << "You Entered : " << inputNumber; return 0; }Output
Enter an integer 4 You Entered : 4
In above program, we declared an integer variable "inputNumber". Then we print a message on screen "Enter an integer", asking user to enter an integer value. When user enters and integer, it gets stored in variable "inputNumber" using cin. Then we print the number entered by user on screen using cout. Below is the brief description of cin and cout stream.
Standard Input Stream (cin) in C++
cin is a predefined object of class istream. cin object by default is attached to the standard input device which is keybord in most programming environments.
cin along with extraction operator (>>) is used to take keyboard input from user.
Here is an example of reading an integer value from user
int count; cin >> count;Here is an example of reading multiple integer value from user
int count, sum; cin >> count >> sum;
Standard Output Stream (cout) in C++
cout is a predefined object of class ostream. cout object by default is attached to the standard output device which is screen in most programming environments. cout along with insertion operator (<<) is used to display data to user on screen.
Here is an example of printing an integer on screenint count = 10; cout << count;We can print multiple values at a time by cascading insertion operator.
cout << count << 20 << "Hello";
Recommended Posts