Ads 468x60px

Tuesday, 1 January 2013

Basic Input Output

The command for standard output in C++ is cout.
It is used with writing cout with insertion operator (two less than signs).

1. cout<<"Your Output";
2. cout<<45;
3. cout<<x;

The first line will print Your Output to the output screen, second line print 45 and the third line will print the value of x in output screen.
The cout out command can be also used as :

1. cout<<"Your name is "<<name<<"and your balance is "<<x;

The command for standard input in C++ is cin.
It is used with overload operator (two greater than signs).

1. cin>>x;

The command will accept the value of x.

Write a program to accept two numbers and display the sum of them.


void main()
    int a,b,sum;
    cout<<"Enter a:\t"; 
    cin>>a; cout<<"Enter b:\t"; 
    sum = a+b;
    cout<<"The sum is "<<sum;
Explanation of the above program
The above program will give you the sum of the two integers which you will enter.
Now as explained before the first two line is the header and then main function starts and the first line is clrscr for clearing the screen.

Then we need three variables two for storing values and third for storing the sum, so we declared the three variables a, b and sum of integer data type.

Then we are giving a message that Enter a, means enter the value of a.

Then we are taking input with the input function cin and storing that value in a.

Same process is for the variable b.

Then we write sum = a + b;

We used two operators here assignment ( = ) and arithmetic ( + ).
The equal to ( = ) is assigning the value of a + b to the variable sum.
The addition ( + ) operator is adding the values of a and b.

Then again we are giving the value of sum as an output with the output function cout with the message that sum is sum.

Then getch function for holding the output screen.
Share if you liked this Post using your favorite sharing service:

Subscribe Updates, Its FREE!


Post a Comment

Related Posts Plugin for WordPress, Blogger...