Вы находитесь на странице: 1из 10

Solutions to Exercises (Chapter No.

3 –
Loops and Decisions)
Object Oriented Programming in C++ by Robert Lafore
Solutions provided by Muhammad Anas

Question No. 1

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
char exit = 'n';
do {
int number;
cout << "Enter a number: ";
cin >> number;
for (int i=1; i<=200; i++)
{
cout << setw(5) << i * number << "\t";//"\t" is escape sequence
for "Tab"
if (i%10 == 0)//if ten numbers have been printed in the current
line then
{ //start a new line
cout << endl;
}
}

cout << "Close this program?(y/n)\n";


cin >> exit;
} while (exit != 'y' && exit != 'Y');
return 0;
}

Question No. 2

#include <iostream>

using namespace std;

int main()
{
char exit = 'n';
do {
int choice;
float f, c;
cout << "1 - Convert Fahrenheit to Celcius\n2 - Convert Celsius to
Fahrenheit\n";
cout << "Type 1 or 2: ";
cin >> choice;
if (choice == 1)
{
cout << "Enter temperature in Fahrenheit: ";
cin >> f;
c = 5.0/9.0*(f - 32);
cout << "In Celsius that's " << c << endl;
}
else if (choice == 2)
{
cout << "Enter temperature in Celsius: ";
cin >> c;
f = (9.0/5.0*c) + 32;
cout << "In Fahrenheit that's " << f << endl;
}
else
{
cout << "Sorry, you entered an invalid choice\n";
}

cout << "Close this program?(y/n)\n";


cin >> exit;
} while (exit != 'y' && exit != 'Y');
return 0;
}

Question No. 3

//this program converts six digits that the user inputs as characters into
//a six digit decimal number.
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
char exit = 'n';
do {
char digit;
double dec_num = 0;
int counter = 1;
cout << "Please enter any six digits to convert them into a decimal
number:\n";
while (counter <= 6)
{
digit = getche();
dec_num = (dec_num * 10) + (static_cast<int>(digit)-48);
counter++;
}
cout << "\nNumber is: " << dec_num << endl;
cout << "Close this program?(y/n)\n";
cin >> exit;
} while (exit != 'y' && exit != 'Y');
return 0;
}

Question No. 4

#include <iostream>

using namespace std;

int main()
{
char exit = 'y';
do {
float first_num, second_num, result;
char operation;
cout << "Enter first number, operator, second number saparated by spaces
and\npress ENTER at the end of Expression:\n";
cin >> first_num >> operation >> second_num;
switch(operation)
{
case '+':
result = first_num + second_num;
cout << "Answer = " << result << endl;
break;
case '-':
result = first_num - second_num;
cout << "Answer = " << result << endl;
break;
case '*':
result = first_num * second_num;
cout << "Answer = " << result << endl;
break;
case '/':
result = first_num / second_num;
cout << "Answer = " << result << endl;
}

cout << "Do another?(y/n) ";


cin >> exit;
} while (exit == 'y' || exit == 'Y');
return 0;
}

Question No. 5

#include <iostream>

using namespace std;

int main()
{
int xes = 1, lines = 20;
int spaces = --lines;
for (int i = 1; i <= lines; i++)
{
for (int j = 1; j <= spaces; j++)
{
cout << " ";
}
for (int k = 1; k <= xes; k++)
{
cout << 'X';
}
cout << "\n";
spaces--;
xes += 2;
}
system("pause");
return 0;
}

Question No. 6

//calculates factorials, demonstrates FOR loop


#include <iostream>
using namespace std;

int main()
{
unsigned int numb;
unsigned long fact; // long for large numbers

cout << "Enter a number: ";


cin >> numb; //get number
do {
fact = 1;
for(int j=numb; j>0; j--) //multiply 1 by
{
fact *= j; //numb, numb-1, ..., 2, 1
}
cout << "Factorial is " << fact << endl;
cout << "Enter a number: ";
cin >> numb; //get number
} while(numb != 0);
return 0;
}

Question No. 7

#include <iostream>

using namespace std;

int main()
{
char exit = 'n';
do {
unsigned long double initial_amount, final_amount;
float interest_rate;
int years;
cout << "Enter initial amount: ";
cin >> initial_amount;
cout << "Enter number of years: ";
cin >> years;
cout << "Enter interest rate (percent per year): ";
cin >> interest_rate;
for (int n=1; n <= years; n++)
{
initial_amount = initial_amount +
((interest_rate/100)*initial_amount);
}
final_amount = initial_amount;
cout << "At the end of " << years << " years, you will have " <<
final_amount << " dollars.\n";

cout << "Close this program?(y/n): ";


cin >> exit;
} while (exit != 'y' && exit != 'Y');
return 0;
}

Question No. 8

#include <iostream>

using namespace std;

int main()
{
char exit = 'n';
do {
int pounds1, pounds2, poundsr, shillings1, shillings2, shillingsr;
float pence1, pence2, pencer;//r in some variable names stands for
"result"
char dot;
cout << "Use space bar to saparate numbers from dots.\n";
cout << "Enter First amount(Pounds.Shiilings.Pence): \x9c";
cin >> pounds1 >> dot >> shillings1 >> dot >> pence1;
cout << "Enter second amount: \x9c";
cin >> pounds2 >> dot >> shillings2 >> dot >> pence2;
pencer = pence1 + pence2;
shillingsr = shillings1 + shillings2;
poundsr = pounds1 + pounds2;
if (pencer >= 12)
{
pencer -= 12;
shillingsr++;
}
if (shillingsr >=20)
{
shillingsr -= 20;
poundsr++;
}
cout << "Total is \x9c" << poundsr << "." << shillingsr << "." << pencer
<< endl;
cout << "Close this program?(y/n)\n";
cin >> exit;
} while (tolower(exit) != 'y');
return 0;
}

Question No. 9

#include <iostream>

using namespace std;

int main()
{
char exit = 'n';
do {
int num_of_guests, num_of_chairs;
double possible_arrangements;
cout << "Please enter number of chairs:\n";
cin >> num_of_chairs;
cout << "Please enter number of guests:\n";
cin >> num_of_guests;
possible_arrangements = num_of_guests;
for (int i = 1; i < num_of_chairs; i++)
{
num_of_guests--;
possible_arrangements *= num_of_guests;
cout << "loop number " << i << " arrangements = " <<
possible_arrangements << endl;
}
cout << "Maximum possible arrangements for the guests are " <<
possible_arrangements << endl;

cout << "Close this program?(y/n)\n";


cin >> exit;
} while (exit != 'y' && exit != 'Y');
return 0;
}

Question No. 10

#include <iostream>

using namespace std;

int main()
{
char exit = 'n';
do {
unsigned long double initial_amount, final_amount, original_amount;
float interest_rate;
int years = 0;
cout << "Enter initial amount: ";
cin >> initial_amount;
original_amount = initial_amount;
cout << "Enter final amount: ";
cin >> final_amount;
cout << "Enter interest rate (percent per year): ";
cin >> interest_rate;
while (initial_amount <= final_amount)
{
initial_amount = initial_amount +
((interest_rate/100)*initial_amount);
years ++;
}
cout << original_amount << " dollars will become " << final_amount << "
dollars in approximately "
<< years << " years.\n";

cout << "Close this program?(y/n): ";


cin >> exit;
} while (exit != 'y' && exit != 'Y');
return 0;
}

Question No. 11

#include <iostream>

using namespace std;

int main()
{
char exit = 'n';
do {
char choice, dot;
int pounds1, pounds2, poundsr, shillings1, shillings2, shillingsr;
float pence1, pence2, pencer, float_num, decimal_pounds;
float fractional_part, decimal_shillings;

cout << "What do you want to do?\n";


cout << "1-Add two old style amounts.\n";
cout << "2-Subtract two old style amounts.\n";
cout << "3-Multiply an old style amount with a floating point
number.\n";
cout << "Please press the appropriate key: ";
cin >> choice;

switch (choice)
{
case '1':
cout << "Enter first amount: \x9c";
cin >> pounds1 >> dot >> shillings1 >> dot >> pence1;
cout << "Enter second amount: \x9c";
cin >> pounds2 >> dot >> shillings2 >> dot >> pence2;

poundsr = pounds1 + pounds2;


shillingsr = shillings1 + shillings2;
pencer = pence1 + pence2;
if (pencer >= 12)
{
shillingsr++;
pencer -= 12;
}
if (shillingsr >= 20)
{
poundsr++;
shillingsr -= 20;
}
cout << "Total is \x9c" << poundsr << dot << shillingsr <<
dot << pencer << endl;
break;
case '2':
cout << "Second amount shall be subtracted from the first
one.\n";
cout << "Enter first amount: \x9c";
cin >> pounds1 >> dot >> shillings1 >> dot >> pence1;
cout << "Enter second amount: \x9c";
cin >> pounds2 >> dot >> shillings2 >> dot >> pence2;

poundsr = pounds1 - pounds2;


shillingsr = shillings1 - shillings2;
pencer = pence1 - pence2;
/*while (poundsr < 0 || shillingsr < 0 || pencer < 0)
{*/
if (pencer < 0)
{
shillingsr--;
pencer = 12 + pencer;
}
if (shillingsr <0)
{
if (poundsr >=0)
{
poundsr--;
}
else
{
poundsr++;
}
shillingsr = 20 + shillingsr;
}
/*}*/
cout << "The result is \x9c" << poundsr << dot << shillingsr
<< dot << pencer << endl;
break;
case '3':
cout << "Enter first amount: \x9c";
cin >> pounds1 >> dot >> shillings1 >> dot >> pence1;
cout << "Enter the floating point number: ";
cin >> float_num;
decimal_pounds = ((((pounds1 * 20) + shillings1) * 12) +
pence1)/(20*12);
decimal_pounds *= float_num;//multiply amount entered with
the floating point number
poundsr = static_cast<int>(decimal_pounds);
fractional_part = decimal_pounds - poundsr;
decimal_shillings = fractional_part * 20;
shillingsr = static_cast<int>(decimal_shillings);
fractional_part = decimal_shillings - shillingsr;
pencer = fractional_part * 12;
cout << "The result is \x9c" << poundsr << dot << shillingsr
<< dot << static_cast<int>(pencer) << endl;
break;
default:
cout << "Invalid input. Please try again!\n";
}
cout << "Close this program?(y/n)\n";
cin >> exit;
} while (tolower(exit) != 'y');
return 0;
}

Question No. 12

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
char exit = 'n';
do {
float a, b, c, d, e, f;
char operation, slash;

cout << "Enter the expression\n";


cin >> a >> slash >> b >> operation >> c >> slash >> d;
switch(operation)
{
case '+':
e = a*d + b*c;
f = b*d;
cout << "Answer = " << e << " / " << f << endl;
break;
case '-':
e = a*d - b*c;
f = b*d;
cout << "Answer = " << e << " / " << f << endl;
break;
case '*':
e = a*c;
f = b*d;
cout << "Answer = " << e << " / " << f << endl;
break;
case '/':
e = a*d;
f = b*c;
cout << "Answer = " << e << " / " << f << endl;
break;
}
cout << "Close this program?(y/n)\n";
cin >> exit;
} while (exit != 'y' && exit != 'Y');
return 0;
}

Вам также может понравиться