OOPinCPP - Chapter 3 - Solutions
OOPinCPP - Chapter 3 - Solutions
OOPinCPP - Chapter 3 - Solutions
3 –
Loops and Decisions)
Object Oriented Programming in C++ by Robert Lafore
Solutions provided by Muhammad Anas
Question No. 1
#include <iostream>
#include <iomanip>
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;
}
}
Question No. 2
#include <iostream>
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";
}
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>
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>
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;
}
Question No. 5
#include <iostream>
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
int main()
{
unsigned int numb;
unsigned long fact; // long for large numbers
Question No. 7
#include <iostream>
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";
Question No. 8
#include <iostream>
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>
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;
Question No. 10
#include <iostream>
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";
Question No. 11
#include <iostream>
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;
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;
Question No. 12
#include <iostream>
#include <conio.h>
int main()
{
char exit = 'n';
do {
float a, b, c, d, e, f;
char operation, slash;