4th day
programming:
Today , we are going to see how to take input,
To take input we use a built in object (a command in form of
an object ) i.e cin (in other word it
stands for compile input i.e. standard input streaming )
To use this feature you use the command of the object
“cin” in upcoming after it it has
the “>>” operator which is extraction or get from operator
Suppose if you want to ask user
to input two int type numbers then
#include<iostream>
Int main()
{
Int num1,num2; /* you can declare more than one variable at the same time in
the same line separated by a (,) */
cout<<”Enter the num1 and
num2”<<endl;
cin>>num1
cin>>num2;
cout<<”\n Entered number
is”<<num1<<” \t ”<<num2;
Return 0;
}
As you can see /* and */ these are the comment lines used by
the programmer to comment on the large scale programming programs such that
they can be easily be understood.
On the next line you display the
output “Enter the num1 and num2 ”.
Then you input num1 and
num2 , (Point to be noted you can also take input as
cin>>num1>>num2; but it will become a little complex)
The 2nd last line you
can see “/n” and “/t” in the quotation ,
These are escape sequence, they have a specific function, like
\n : for next line
\t : for five space
jump
\a : bell
\b : backspace
\f : form-feed
You can use them in the
message but they won’t appear instead their specific function will be
performed.
If you want to print the sum you can also print the sum using the arthematic operation (+,-,*,/)
as it work same on the calculator as well as on the computer since for the sum if you want to print the sum it will be in 2 step.
1)declare the two int or float or double type variable
2) intialize the two variables
3) using both variable use the arthematic symbol in between them (variable1+variable2);
4)cout the sum of them.
e.g
double no1,no2;
cout<<"Enter the number 1 "<<endl;cin>>no1;
cout<<"Enter the number 2"<<endl;cin>>no2;
cout<<"\n sum is :"<<(no1+no2);
or you can also
cout<<"Enter the number 1 "<<endl;cin>>no1;
cout<<"Enter the number 2"<<endl;cin>>no2;
double sum=no1+no2;
cout<<"\n sum is :"<<sum;
I hope you have understand this easily now its your task to make a calculator of 4 function?
hint:
use same as above ,just change the operations :)
________________________________________________________________________________
Now the thing arises if you want user to ente the number as well as the arthematic symbole (+,-,*,/)
then condition is required
for that there is a keyword object names as if
Writing style :
if(condition)
statment;
else
statment;
or
if (condition)
{statments;
}
else
{
statments;
}
the if keyword check the condition provided in the parenthesis like
int no=5;
int no2=6;
if(no2>no)
{
cout<<"No 2 is greater"<<endl;
}
else
{
cout<<"no1 is greater"<<endl;0
}
if you concentrate in this if else statment the thing which is happening is that
if is the keyword it check the condition.
is no2 greater than no ???? yes then the statment below if is printed and the else is skipped . :)
SO let suppose if no2 was 5 n no was 6 then condition of if statment will be false hence , else body will be executed so (no 1 is greater will print )
this is the basic decision case for if else statments.
No comments:
Post a Comment