# Object Oriented Programming in C++ By Robert Lafore - Chapter 3 Complete Solution - C++ oop

Program 3.1:

// Code By HI //
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
int num,tab=0;

cout<<"Enter Number: ";
cin>>num;

for (int row=1; row<=20; row++)
{
for (int col=1; col<=10; col++)
{
tab=tab+num;
cout<<setw(6)<<tab;
}
cout<<endl;
}

return 0;
}

OUTPUT:

Program 3.2:

//Temperature converter using do-while loop//
// Code By HI //
#include <iostream>
using namespace std;
int main ()
{
float temp, cel, fah;
int type;
do
{
cout<<"Type '1' to convert Fahrenheit to Celcius, \nType 2 to convert Celcius to Fahrenheit: ";
cin>>typ;

if (typ ==1)
{
cout<<"Enter Temperature in Fahrenheit: ";
cin>>temp;

cel=(temp-32)*5/9;

cout<<temp<<" Fahrenheit = "<<cel<<" Celcius"<<endl;
break;
}

else if (typ ==2)
{
cout<<"Enter Temperature in Celcius: ";
cin>>temp;

fah=(temp*9/5)+32;

cout<<temp<<" Celcius = "<<fah<<" Fahrenheit"<<endl;
break;
}
}
while (typ != '1' || typ !='2');

return 0;
}

OUTPUT:

// Code By HI //
#include <iostream>
#include <conio.h>
using namespace std;
int main ()
{
char ch;
long total=0;
cout<<"Enter Number: ";
while ((ch=getche())!='\r')
total=total*10 + ch-'0';

cout<<"Number is: "<<total;
return 0;
}

OUTPUT:

Program 3.4:

// Code By HI //
#include <iostream>
using namespace std;
int main ()
{
float a,b;
char op, dis;
do
{
cout<<"Enter The First Number: ";
cin>>a;
cout<<"Choose Operator (/,+,-,*): ";
cin>>op;
cout<<"Enter The Second Number: ";
cin>>b;
switch (op)
{
case '+':
break;
case '-':
cout<<"Subtraction is: "<<a-b<<endl;
break;
case '*':
cout<<"Multiplication is: "  <<a*b<<endl;
break;

case '/':
cout<<"Division is: "<<a/b<<endl;
break;
}

cout<<"Do you want to solve another problem? (y/n)";
cin>>dis;
}
while (dis != 'n');

return 0;
}

OUTPUT:

// Code By HI //
#include <iostream>
using namespace std;
int main ()
{
int lim;
cout<<"Enter Limit: ";
cin>>lim;
for (int row=1; row<=lim; row++)
{
for (int j=lim; j>=row; j--)
{
cout<<" ";
}
for (int k=1; k<2*row; k++)
{
cout<<"x";
}
cout<<endl;
}

return 0;
}

OUTPUT:

// Code By HI //
#include <iostream>
using namespace std;
int main ()
{
int num;

do
{
cout<<"Enter ZERO to exit OR Enter a positive number: ";
cin>>num;
int fact =1;

for (int i=num; i>=1; i--)
{
fact=fact*i;
}
cout<<"Factorial is: "<<fact<<endl;

}
while (num != 0);

return 0;
}

OUTPUT:

Program 3.7:

// Code By HI //
#include <iostream>
using namespace std;
int main ()
{
float amount, years, interest, profit;
cout<<"Enter Initial Amount: ";
cin>>amount;
cout<<"Enter Number of Years: ";
cin>>years;
cout<<"Enter Interest Percentage: ";
cin>>interest;
for (int i=years; i>0; i--)
{
profit= amount + (amount*(interest/100));
amount=profit;
}
cout<<"At the end of "<<years<<" Years, Total amount left will be: "<<profit<<endl;
return 0;
}

OUTPUT:

Program 3.8:

// Code By HI //
#include <iostream>
using namespace std;
int main ()
{
int p1, p2, s1, s2, pen1, pen2, pound, shelling, pence;
char point, dec;
do
{
cout<<"Enter First Amount: ";
cin>>p1>>point>>s1>>point>>pen1;
cout<<"Enter Second Amount: ";
cin>>p2>>point>>s2>>point>>pen2;
pound= p1+p2;
shelling= s1+s2;
pence= pen1+pen2;
if (pence>11)
{
shelling=shelling+(pence/12);
pence=pence%12;
}
else if (shelling>19)
{
pound=pound+(shelling/20);
shelling=shelling%20;
}
cout<<"Total Amount Is: "<<pound<<" . "<<shelling<<" . "<<pence<<endl;
cout<<"Do you want to repeat the action? (y/n): ";
cin>>dec;
}

while (dec == 'y');

return 0;
}

OUTPUT:

Program 3.9:

// Code By HI //
#include <iostream>
using namespace std;
int main ()
{
int guests, chairs, x=1;
cout<<"Enter the number of guests: ";
cin>>guests;
cout<<"Enter the number of chairs: ";
cin>>chairs;
if (guests>chairs)
{
for (int i=chairs; i>0; i--)
{
x=x*guests;
guests--;
}
}
cout<<"Number of possible arrangements are: "<<x<<endl;
return 0;
}

OUTPUT:

Program 3.10:

// Code By HI //
#include <iostream>
using namespace std;
int main ()
{
float total_amount, amount, interest, x;
int year=0;
cout<<"Enter Total Amount With Profit: ";
cin>>total_amount;
cout<<"Enter Initial Amount: ";
cin>>amount;
cout<<"Enter Interest Percentage: ";
cin>>interest;
x= amount;
while (x<total_amount)
{
x= x*(1+(interest/100));
year++;
}
cout<<"It will take "<<year<<" Years!"<<endl;
return 0;
}

OUTPUT:

Program 3.11:

#include <iostream>
// Code By HI //
using namespace std;
int main ()
{
int p1,p2,s1,s2,pen1,pen2, pound, shelling, pence;
char point, dec, op;
do
{
cout<<"Enter First Amount: ";
cin>>p1>>point>>s1>>point>>pen1;
cout<<"Enter Second Amount: ";
cin>>p2>>point>>s2>>point>>pen2;
cout<<"Choose The Operator to perform the function (+, -, *): ";
cin>>op;
switch (op)
{
case '+':
pound= p1+p2;
shelling= s1+s2;
pence= pen1+pen2;
break;

case '-':
pound= p1-p2;
shelling= s1-s2;
pence= pen1-pen2;
break;
case '*':
pound= p1*p2;
shelling= s1*s2;
pence= pen1*pen2;
break;
default:
cout<<"Invalid Operator is chosen! "<<endl;
break;
}
if (pence>11)
{
shelling=shelling+(pence/12);
pence=pence%12;
}
else if (shelling>19)
{
pound=pound+(shelling/20);
shelling=shelling%20;
}
cout<<"Total Amount Is: "<<pound<<" . "<<shelling<<" . "<<pence<<endl;
cout<<"Do you want to repeat the action? (y/n): ";
cin>>dec;
cout<<endl;

}

while (dec == 'y');
cout<<endl;

return 0;
}

OUTPUT:

Program 3.12:

#include <iostream>
// Code By HI //
using namespace std;
int main ()
{
int a,b,c,d;
char op, frac, dec;
do
{
cout<<"Enter First Fraction: ";
cin>>a>>frac>>b;
cout<<"Enter Second Fraction: ";
cin>>c>>frac>>d;
cout<<"Choose Operator (+, -, /, *) : ";
cin>>op;
switch (op)
{
case '+':
break;
case '-':
cout<<"Subtraction is: "<<((a*d)-(b*c))/(b*d)<<endl;
break;

case '/':
cout<<"Division is: "<<(a*d)/(b*c)<<endl;
break;
case '*':
cout<<"Multiplication is: "<<(a*c)/(b*d)<<endl;
break;
default:
cout<<"Invalid Operator Chose! "<<endl;
break;
}

cout<<"Do You Want To Continue? (y/n) : ";
cin>>dec;

}
while (dec == 'y');
return 0;
}

OUTPUT: