For any programming language, two of the most important operations are its input and output operations. In our first program, we used output operator to print hello world. In this article, we will study about both the operators and write a program that inputs a value and provides a proper result using the output operator.

Getting an input is nearly similar to getting an output in C++. We have different signatures for each of the operators. >> is the input operator and << is the output operator. We use cin for inputting the value and cout for outputting the value. For instance,

cin>>a;
cout<<"This is cout";


So the first line of code will ask the user to input the value. Now depending on the data type of a, the value will be assigned to a. If a is an integer type, the value that will be demanded will be integer type and that value will be assinged to a.

After you enter the value and hit enter, the cout operator will work and will print the line “This is cout”. We can see the output of the program below.

5
This is cout


You can always insert the value in the variable, do some operations and output the demanded result. Let us make a program that inputs principal amount(P), rate of interest(R) and time(T) and calculate the simple interest on our money.

In this case, our inputs and output variables are

• Principal amount – p
• Interest rate -r
• Time – t
• Output – o
#include<iostream>
using namespace std;
int main(){

int p,r,t,o;
/* here we are considering all of our
variables as integer data types*/
cout<<"Welcome to SI calculator"<<endl;
cin>>p;
cin>>r;
cout<<"Enter the time in years"<<endl;
cin>>t;
//formula for interest rate
o = p+p*r*t/100;
cout<<"SI : "<<o<<endl;
return 0;
}


The output of the program is

Welcome to SI calculator
2000
5
Enter the time in years
3
SI : 2300


So, there you have it. A simple program that calculates simple interest given proper input parameters. Here are some questions that you can try

Write a program that prints the letter of your initials in a grid of stars. For instance, my initial letter is A.

                            *
*  *
*    *
*      *
* * * ** *
*          *
*            *


Hint: Use cout and spaces

Write a program that outputs the sum of two input numbers.

Amar//Input
{"ft_lang_label":"__label__en","ft_lang_prob":0.8309252,"math_prob":0.9719011,"size":2537,"snap":"2021-31-2021-39","text_gpt3_token_len":592,"char_repetition_ratio":0.15238847,"word_repetition_ratio":0.015317286,"special_character_ratio":0.25226647,"punctuation_ratio":0.105058365,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99435884,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-08-04T06:07:38Z\",\"WARC-Record-ID\":\"<urn:uuid:edefecb7-e821-4ed6-9e92-eef78b125d99>\",\"Content-Length\":\"31680\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:8f2ae63a-a186-4941-8a43-84c7b40edca0>\",\"WARC-Concurrent-To\":\"<urn:uuid:5a4a14be-cf72-4d70-b124-7dfb55f28ced>\",\"WARC-IP-Address\":\"166.62.27.147\",\"WARC-Target-URI\":\"http://mycodepartner.com/cplusplus/cin-and-cout/\",\"WARC-Payload-Digest\":\"sha1:SETSLI6SQPKFK7PEUBTCFXZYSWXUV3KW\",\"WARC-Block-Digest\":\"sha1:GCPTGIGIY5SYI47YRKC54GQHYHMXE7RK\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-31/CC-MAIN-2021-31_segments_1627046154796.71_warc_CC-MAIN-20210804045226-20210804075226-00579.warc.gz\"}"}