You are on page 1of 16

#include<iostream>

using namespace std;


int main()
{
int var1,var2;//variable declaration
var1=10;
var2=20;
cout<<“Number1=”;
cout<<var1<<endl;
cout<<“Number2=”<<var2;
return 0;
}

1
Assignment statement

Eg. int var1=10;


int var2=20;
#include<iostream>
using namespace std;
int main()
{
int var1=10;
int var2=20;
int var3=var1+var2;
cout<<var1<<“+”<<var2<<“=”<<var3;
return 0;
}

2
Character Variable( char )
- Character constants use single quotation mark around a character.
(Eg. ‘a’, ‘b’)

#include<iostream>
using namespace std;
int main()
{
char var1=‘A’;
char var2=‘B’;
cout<<var1<<endl<<var2;
return 0;
}

3
Escape Sequence
Escape Sequence Character
\a Bell cout<<“\a”;
\b Backspace cout<<“Hello\b”;
\f formfeed
\n Newline
\r Return(Enter)
\t Tab
\\ Backslash cout<<“\\x”;
\’ Single quotation cout<<“I \’m”;
\” Double quotation cout<<“I \”m”;
\xdd Hexadecimal notation cout<<“\x9c”;

4
#include<iostream>
using namespace std;
int main()
{
char var1=‘A’;
char var2=‘\t’;
cout<<var1;
cout<<var2;
var1=‘B’;
cout<<var1;
cout<<“\n”;
return 0;
}

5
Input with cin

#include<iostream>
using namespace std;
int main()
{
int num;
cout<<“Enter a number”;
cin>>num;
cout<<“Your entered number is”<<num<<endl;
return 0;
}

6
#include<iostream>
using namespace std;
int main()
{
int num1;
int num2;
int num3;
cout<<“Enter a number”;
cin>>num1;
cout<<“Enter a number”;
cin>>num2;
num3=num1+num2;
cout<<num1<<“+”<<num2<<“=”<<num3<<endl;
return 0;
}

7
Floating Point Type(float)
- Three kind of float types: float, double, long double

#include<iostream>
using namespace std;
int main()
{
float radius,area;
float pi=3.14;
cout<<“Enter a radius”;
cin>>radius;
area=pi*radius*radius;
cout<<“Area=“<<area<<endl;
return 0;
} 8
The setw manipulator
- Used with insertion operator(<<)
- Only cin/cout statement
- Is in a separate header file called I,O,M,A,N,I,P
- setw(n) where n is the argument of setw() function
- The value is right justified with the field

Setfill() manipulator
- Use setfill with setw()
- Setfill takes a single character as an argument
#include<iomanip>
#include<iostream>
using namespace std;
int main()
{
cout<<“Computer“<<setw(20)<<setfill(‘.’)<<“University”<<endl;
return 0;
}
9
Arithmetic operators and arithmetic assignment operators are binary operator.
Increment operators are unary operator.

Unary Operator
prefix(++x)
Increment operator(++)
postfix(x++)
Eg. int x=10;
cout<<++x; //11
cout<<x++; //11
cout<<x; //12
prefix(--)
Decrement operator(--)
postfix(--)
Eg. int x=10;
cout<<--x; //9
cout<<x--; //9
cout<<x; //8

10
Assignment
1.Write a program that displays your favorite poem. Use an appropriate escape
sequence for the line breaks. If you don’t have a favorite poem, you can
borrow this one by Ogden Nash: Candy is dandy, But liquor is quicker.
#include<iostream>
using namespace std;
int main()
{
cout<<“Candy is dandy, \n But liquor is quicker.”<<endl;
return 0;
}

11
2.There are 7.481 gallons in a cubic foot, write a program that asks the user to
enter a number of gallons, and then displays the equivalent in cubic feet.
1. #include<iostream>
using namespace std;
int main()
{
float gallons,cubicfeet;
cout<<“Enter gallons”;
cin>>gallons;
cubicfeet=gallons/7.481;
cout<<“Equivalent in cubic feet=”<<cubicfeet;
return 0;
}

12
3. Write a program that generates the following output:
10
20
19
Use an integer constant for the 10, an arithmetic assignment operator to
generate the 20, and a decrement operator to generate the 19.
#include<iostream>
using namespace std;
int main()
{
int x=10;
cout<<x<<endl;
x*=2;
cout<<x<<endl;
cout<< --x;
return 0;
}
13
4.When a value is smaller than a field specified with setw(),the unused
locations are, by default, filled in with spaces. The manipulator setfill() takes
a single character as an argument and causes this character to be substituted
for spaces in the empty parts of a field. Rewrite the WIDTH program so that
the characters on each line between the location name and the population
number are filled in with periods instead of spaces, as in Portcity.....2425785
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
long pop1=2425785;
cout<<"Portcity"<<setw(12)<<setfill('.')<<pop1;
return 0;
}
14
5.If you have two fractions, a/b and c/d, their sum can be obtained from the
formula
a/b+c/d=(a*d+b*c)/b*d
Write a program that encourages the user to enter two fractions, and then
displays their sum in fractional form. (You don’t need to reduce it to lowest
terms.) The interaction with the user might look like this:
Enter first fraction: 1/2
Enter second fraction: 2/5
Sum = 9/10

15
#include<iostream>
using namespace std;
int main()
{
int a,b,c,d,e,f;
char dummychar;
cout<<"Enter first fraction";
cin>>a>>dummychar>>b;
cout<<"Enter second fraction";
cin>>c>>dummychar>>d;
e=a*d+c*b;
f=b*d;
cout<<e<<dummychar<<f<<endl;
return 0;
}

16

You might also like