Prefix ADD : , SUB :, MUL :, DIV : to each results as specified. Each result is
displayed in separate lines. Example is given below.
Code:
#include<iostream>
using namespace std;
int main()
{
int b,x,y,i,a,s,m,d;
cout<<"1) Addition\n";
cout<<"2) Subtraction\n";
cout<<"3) multiplication\n";
cout<<"4) division\n";
cout<<"Enter the set of numbers to be entered\n";
cin>>b;
for(i=0;i<b;i++)
{
cout<<"enter the "<<i+1<<"th set of number\n";
cin>>x>>y;
a=x+y;
s=x-y;
m=x*y;
d=x/y;
cout<<"Sum="<<a<<"\n";
cout<<"difference="<<s<<"\n";
cout<<"product="<<m<<"\n";
cout<<"quotient="<<d<<"\n";
}
}
1
Output:
1) Addition
2) Subtraction
3) multiplication
4) division
Enter the set of numbers to be entered
2
enter the 1th set of number
2
3
Sum=5
difference=-1
product=6
quotient=0
enter the 2th set of number
56
44
Sum=100
difference=12
product=2464
quotient=1
2
Exercise 1.2
Hint: Prime numbers are those numbers which is divisible only by that number
and by one.
Eg: 5 is a prime number where as 4 is not.
Code:
#include<iostream>
using namespace std;
int main()
{
int a,b,i,j,k=0;
cout<<"Enter the two numbers\n";
cin>>a>>b;
cout<<"the prime numbers are:\n";
for(i=a;i<=b;i++)
{
for(j=2;j<i;j++)
{
if(i%j==0)
{
k++;
}
}
if(k==0)
cout<<i<<"\n";
k=0;
}
}
3
Output:
4
Exercise 1.3
Code:
#include<iostream>
using namespace std;
#include<math>
int main()
{
int n,i,rem,sum=0;
cout<<"Enter the number\n";
cin>>n;
for(i=0;n>0;i++)
{
rem=n%10;
rem=(rem+7)%10;
sum+=rem*pow(10,i);
n=n/10;
}
cout<<"The encrypted code is:"<<sum<<"\n";
}
Output:
Enter the number
2345
The encrypted code is:9012
5
Exercise 2.1
Code:
#include<iostream>
using namespace std;
int main()
{
int a,n=0,b,c,count=0,i;
cout<<"enter the number:\n";
cin>>a;
for(i=0;a%10==0;i++)
{
count++;
a=a/10;
}
while(a!=0)
{
b=a%10;
n=n*10+b;
a=a/10;
}
while(n!=0)
{
c=n%10;
n=n/10;
switch(c)
{
case 1:
cout<<"ONE";
break;
case 2:
cout<<"TWO";
break;
case 3:
6
cout<<"THREE";
break;
case 4:
cout<<"FOUR";
break;
case 5:
cout<<"FIVE";
break;
case 6:
cout<<"SIX";
break;
case 7:
cout<<"SEVEN";
break;
case 8:
cout<<"EIGHT";
break;
case 9:
cout<<"NINE";
break;
case 0:
cout<<"ZERO";
break;
}
c=0;
cout<<"\t";
}
for(i=0;i<count;i++)
{
cout<<"ZERO\t";
}
}
Output:
7
Exercise 2.2
Code:
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter the number of transaction:";
cin>>n;
int a[n],i,b,c,k;
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Enter the transaction to be removed and entered:";
cin>>b;
cin>>c;
for(i=0;i<n;i++)
{
if(b==a[i])
{
a[i]=0;
k=i;
}
}
cout<<"Transaction "<<b<<" is found and processed ";
cout<<"the remaining transactions are:\n";
for(i=0;i<n;i++)
{
if(a[i]!=0)
cout<<a[i]<<"\t";
}
cout<<"\nTransaction "<<c<<" is not found and added to the transaction
list\n";
a[k]=c;
cout<<"New list of transaction are:\n";
8
for(i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
Output:
9
Exercise 2.3
Code:
#include<iostream>
#include<stdio>
#include<string>
using namespace std;
int main()
{
char ms[50],ss[20];
int i,j=0,s=0;
cout<<"Enter the mainstring:\n";
gets(ms);
cout<<"Enter the substring\n";
cin>>ss;
for(i=0;i<strlen(ms);i++)
{
if(ms[i]==ss[j])
{
j++;
if(j==strlen(ss))
{
s=1;
break;
}
}
else
j=0;
}
if(s==1)
cout<<"The given word is present.\n";
else
cout<<"The given word is not present.\n";
}
Output:
SGPA:
∑(Ci × Gp i ) where C = the credit for ith subject , Gp = Grade
∑Ci
i i
Code:
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
void rno();
struct stud
{int rno;
char name[20];
char g[7][2];
float SGPA;
}ob[3],temp;
int main()
{int i=0,j,k=0;
float gp;
for(i=0;i<3;++i)
{cout<<"Enter details of student "<<i+1<<"\n";
cout<<"Enter rno:";
cin>>ob[i].rno;
11
cout<<"Enter name of the student:";
getchar();
gets(ob[i].name);
cout<<"Enter grades for five subjects and 2 labs:";
for(j=0;j<7;++j)
{//for(k=0;k<2;++k)
gets(ob[i].g[j]);
//getchar();
}
}
for(i=0;i<3;++i)
{ gp=0;
for(j=0;j<7;++j)
{ if(ob[i].g[j][0]=='A'&&ob[i].g[j][1]=='+')
{if(j<5)
gp+=40;
else
gp+=20;
}
else if(ob[i].g[j][0]=='A'&&ob[i].g[j][1]=='-')
{if(j<5)
gp+=36;
else
gp+=18;
}
else if(ob[i].g[j][0]=='A')
{if(j<5)
gp+=40;
else
gp+=20;
}
if(ob[i].g[j][0]=='B'&&ob[i].g[j][1]=='+')
{if(j<5)
gp+=32;
else
gp+=16;
}
if(ob[i].g[j][0]=='B'&&ob[i].g[j][1]=='-')
12
{if(j<5)
gp+=28;
else
gp+=14;
}
else if(ob[i].g[j][0]=='B')
{if(j<5)
gp+=30;
else
gp+=15;
}
if(ob[i].g[j][0]=='C'&&ob[i].g[j][1]=='+')
{if(j<5)
gp+=26;
else
gp+=13;
}
if(ob[i].g[j][0]=='C'&&ob[i].g[j][1]=='-')
{if(j<5)
gp+=22;
else
gp+=11;
}
else if(ob[i].g[j][0]=='C')
{if(j<5)
gp+=24;
else
gp+=12;
}
if(ob[i].g[j][0]=='D')
{if(j<5)
gp+=20;
else
gp+=10;
}
else if(ob[i].g[j][0]=='F')
13
{if(j<5)
gp+=0;
else
gp+=0;
}
}
ob[i].SGPA=gp/24;
rno();
}
void rno()
{int a[3],j,i;
for(i=0;i<3;++i)
{for(j=0;j<3-i-1;++j)
if(ob[j].rno>ob[j+1].rno)
{temp=ob[j];
ob[j]=ob[j+1];
ob[j+1]=temp;
}
}
cout<<"Sorted on the basis of roll no. :\n";
//set iosflags(ios::left);
//---------------------------------------------
cout<<"Roll no"<<setw(24)<<"Name"<<setw(24)<<"SGPA"<<endl;
for(i=0;i<3;++i)
{cout<<"-----------------------------------------------------------------------\
n";
cout<<ob[i].rno<<" "
<<ob[i].name<<setw(24)<<ob[i].SGPA<<endl;
}
14
cout<<"Roll no"<<setw(24)<<"Name"<<setw(24)<<"SGPA"<<endl;
for(i=0;i<3;++i)
{cout<<"-----------------------------------------------------------------------\
n";
cout<<ob[i].rno<<" "
<<ob[i].name<<setw(24)<<ob[i].SGPA<<endl;
}
cout<<"\n";
Output:
15
A-
Sorted on the basis of roll no. :
Roll no Name SGPA
-----------------------------------------------------------------------
12 asish 9
-----------------------------------------------------------------------
23 sachin 10
-----------------------------------------------------------------------
64 vineeth 10
16
Exercise 3.2
Code:
#include<iostream>
using namespace std;
void callbyvalue(int x,int y);
void callbyref(int *,int *);
int main()
{
int a,b,ch=0;
while(ch!=3)
{
cout<<"CALL BY VALUE---------> 1\n";
cout<<"CALL BY REFERENCE-----> 2\n";
cout<<"EXIT------------------> 3\n";
cout<<"Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter two integers\na = ";
cin>>a;
cout<<"b = ";
cin>>b;
callbyvalue(a,b);
cout<<"\nAfter function call\na = "<<a<<"\nb = "<<b<<endl;
break;
case 2:
cout<<"Enter two integers\na = ";
cin>>a;
cout<<"b = ";
cin>>b;
17
callbyref(&a,&b);
cout<<"\nAfter function call\na = "<<a<<"\nb = "<<b<<endl;
break;
case 3:
break;
default:
cout<<"Wrong selection!!";
break;
}
}
}
Output:
CALL BY VALUE---------> 1
CALL BY REFERENCE-----> 2
EXIT------------------> 3
Enter your choice: 2
Enter two integers
a=2
b=3
Inside the function
a=3
b=2
After function call
a=3
b=2
CALL BY VALUE---------> 1
CALL BY REFERENCE-----> 2
EXIT------------------> 3
18
Enter your choice: 1
Enter two integers
a=2
b=3
Inside the function
a=3
b=2
After function call
a=2
b=3
CALL BY VALUE---------> 1
CALL BY REFERENCE-----> 2
EXIT------------------> 3
Enter your choice: 3
19
Exercise 3.3
Tiling Problem
Mr. Sridhar wants to tile his room of specified length and width. Write a program
to help him that finds the number of tiles required to perform the task. Your
program should have the following features.
a) Use a structure Distance to store distances in terms feet and inches (both
are integers).
b) Length and width are the fields of Room structure (each of type
Distance structure).
c) Define a function Count that takes Room structure as argument and
returns the tile-count.
Code:
#include<iostream>
using namespace std;
struct dist
{
int ft;
int inch;
};
struct room
{
dist length,width;
};
int main()
{
room r;
int n;
dist d1,d2,fi;
20
cout<<"Enter the length opf the room: ";
cout<<"Feet: ";
cin>>r.length.ft;
cout<<"Inch: ";
cin>>r.length.inch;
cout<<"Enter the width of the room: ";
cout<<"Feet: ";
cin>>r.width.ft;
cout<<"Inch: ";
cin>>r.width.inch;
n=tilecount(r.length.ft,r.length.inch,r.width.ft,r.width.inch);
cout<<"The no of tiles needed for the specified room is "<<n;
21
for(int i=0;i<sum;i++)
{
x.inch++;
if(x.inch==12)
{ x.inch=0;
x.ft++;
}
}
return(x);
}
Output:
22
Exercise 4.1
Code:
#include<iostream>
using namespace std;
class Complex
{
private:
int real;
int img;
public:
void read();
void display();
void add(Complex);
void mul(Complex);
};
void Complex::read()
{
cout<<"Enter the real part: ";
cin>>real;
cout<<"Enter the imaginary part: ";
cin>>img;
}
void Complex::display()
{
cout<<"The complex no. is "<<real<<"+i"<<img<<endl;
}
23
void Complex::add(Complex c)
{
Complex s;
s.real=real+c.real;
s.img=img+c.img;
s.display();
}
void Complex::mul(Complex c)
{
Complex p;
p.real=(real*c.real)-(img*c.img);
p.img=(img*c.real)+(real*c.img);
p.display();
}
int main()
{
Complex c1,c2;
c1.read();
c2.read();
cout<<"\tSUM"<<endl;
c1.add(c2);
cout<<"\tPRODUCT"<<endl;
c1.mul(c2);
}
Output:
24
Exercise 4.2
Code:
#include<iostream>
using namespace std;
class Rectangle
{
private:
int length;
int breadth;
public:
Rectangle();
Rectangle(int l,int b);
void setvalue();
void area();
};
Rectangle::Rectangle()
{
length = 0;
breadth = 0;
}
Rectangle::Rectangle(int l,int b)
{
length = l;
breadth = b;
}
void Rectangle::setvalue()
{
cout<<"Enter the length of rectangle: ";
25
cin>>length;
cout<<"Enter the breadth of rectangle: ";
cin>>breadth;
}
void Rectangle::area()
{
cout<<"The area of the rectangle is "<<length*breadth<<endl;
}
int main()
{
Rectangle r1,r2,r3,r4;
cout<<"Rectangle 1"<<endl;
r1.area();
r2 = Rectangle(3,4);
cout<<"Rectangle 2"<<endl;
r2.area();
cout<<"Rectangle 3"<<endl;
r3.setvalue();
r3.area();
cout<<"Rectangle 4"<<endl;
r4 = r2;
r4.area();
}
Output:
Rectangle 1
The area of the rectangle is 0
Rectangle 2
The area of the rectangle is 12
Rectangle 3
Enter the length of rectangle: 5
Enter the breadth of rectangle: 2
The area of the rectangle is 10
Rectangle 4
The area of the rectangle is 12
26
Exercise 4.3
Counter Problem
Create a class called Counter. The counter class has one private data member to
hold the counter value. It has three member functions. Init to initialize the data
member, count to increment the counter whenever needed and display to
display the value of the counter. The counter works until the user wants to count.
Design a menu driven program.
Code:
#include<iostream>
using namespace std;
class Counter
{
private:
int counter;
public:
void init();
void count();
void display();
};
void Counter::init()
{
cout<<"Enter a value for the counter: ";
cin>>counter;
}
void Counter::count()
{
counter++;
}
void Counter::display()
{cout<<"The present value of counter is "<<counter<<endl;}
int main()
{
Counter c;
27
c.init();
c.count();
c.display();
}
Output:
28