C++ Basic Input - Output & More - C++ Tutorials For Beginners #5 - Code With Harry
C++ Basic Input - Output & More - C++ Tutorials For Beginners #5 - Code With Harry
Input stream
In the input stream, the direction of the flow of bytes occurs from the input device (for ex keyboard) to the main
memory.
Output stream
In output stream, the direction of flow of bytes occurs from main memory to the output device (for ex-display)
In this piece of code, we have declared two integer variables "num1" and "num2". Firstly we used "cout" to print
"Enter the value of num1:" as it is on the screen, and then we used "cin" to take the input in "num1" at run time
from the user.
Secondly, we used "cout" to print "Enter the value of num2:" as it is on the screen, and then we used "cin" to
take the input in "num2" at run time from the user.
In the end, we used "cout" to print "The sum is" as it is on the screen and also gave the literal "num1+num2"
which will add the values of both variables and print it on the screen.
We have executed our program two times, which can be seen in figure 2. In our 1st execution, we had input the
value "54" for the variable "num1" and value "4" for the variable "num2". This gives us the sum of both numbers
as "58".
In our 2nd execution, we had input the value "5" for the variable "num1" and value "8" for the variable "num2". This
gives us the sum of both numbers as "13".
Important Points
1. The sign "<<" is called insertion operator
2. The sign ">>" is called extraction operator
3. "cout" keyword is used to print
4. "cin" keyword is used to take input at run time.
# include<iostream>
using namespace std;
int main()
{
int num1, num2;
cout<<"Enter the value of num1:\n"; /* '<<' is called Insertion operator */
cin>>num1; /* '>>' is called Extraction operator */
return 0;
}
← Previous Next →
© 2020-2021 CodeWithHarry.com
Back to top | Privacy | Terms