Sunteți pe pagina 1din 5

Phillip Lee

Chapter 3 Programming Challenges

1. Miles per Gallon


Write a program that calculates a cars gas mileage. The program should ask the
user to enter the number of gallons of gas the car can hold and the number of miles
it can be driven on a full tank. It should then calculate and display the number of
miles per gallonthe car gets.

Code:

#include <iostream>
using namespace std;
int main()
{
int gallonsOfGas, numberOfMilesPerTank, MPG;
cout << "How much gallons of gas can your car hold?\n";
cin >> gallonsOfGas;
cout << "How many miles can your car drive on a fulltank?\n";
cin >> numberOfMilesPerTank;
MPG = numberOfMilesPerTank / gallonsOfGas;
cout << "Your MPG is " << MPG << " Miles Per Gallon" <<
endl;
system("pause");
return 0;
}

2. Stadium Seating
There are three seating categories at a stadium. For a softball game, Class A seats
cost $15, Class B seats cost $12, and Class C seats cost $9. Write a program that
asks how many tickets for each class of seats were sold, then displays the amount
of income generated from ticket sales. Format your dollar amount in a xed-point
notation with two decimal points and make sure the decimal point is always
displayed.

Code:

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double ClassA, ClassB, ClassC, totalIncome;
cout << "How many Class A tickets were sold?\n";
cin >> ClassA;
cout << "How many Class B tickets were sold?\n";
cin >> ClassB;
cout << "How many Class C tickets were sold?\n";
cin >> ClassC;
cout << xed;
cout << setprecision(2);
totalIncome = ClassA * 15 + ClassB * 12 + ClassC * 9;
cout << "Total Income Generated: $" << totalIncome <<
endl;
system("pause");
return 0;
}

3. Housing Costs
Write a program that asks the user to enter their monthly costs for each of the
following housing related expenses:

rent or mortgage payment


utilities
Phones
cable

The program should then display the total monthly cost of these expenses, and the
total annual cost of these expenses.

Code:

//This Program Calculates Living Expenses


//**Phillip Lee**
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double rentOrMortgage, utilities, phoneBill, cableBill,
monthlyExpenses, annualExpenses;
cout << "How much is your rent or mortgage?\n";
cin >> rentOrMortgage;
cout << "How much is your utilities?\n";
cin >> utilities;
cout << "How much is your phone bill?\n";
cin >> phoneBill;
cout << "How much is your cableBill?\n";
cin >> cableBill;
cout << xed;
cout << setprecision(2);

monthlyExpenses = rentOrMortgage + utilities + phoneBill +


cableBill;
annualExpenses = monthlyExpenses * 12;
cout << "Your total Monthly Expenses: $" <<
monthlyExpenses << endl;
cout << "Annual Expenses: $" << annualExpenses << endl;
system("pause");
return 0;
}

4. How Much Insurance?


Many nancial experts advise property owners to insure their homes or buildings for
at least 80 percent of the amount it would cost to replace the structure. Write a
program that asks the user to enter the replacement cost of a building and then
displays the minimum amount of insurance that should be purchased for the
property.

Code:

//This Program Calculates the Recommended Insurance for a


Property.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float replaceCostOfProperty, minimumInsurance;
cout << "How much is the replacesment cost or your
property?\n";
cin >> replaceCostOfProperty;
cout << xed;
cout << setprecision(2);
minimumInsurance = replaceCostOfProperty * 0.8;

cout << "Your Minimum Insurance is $" <<


minimumInsurance << endl;
system("pause");
return 0;
}

S-ar putea să vă placă și