Programming Challenge Chapter 4 – Q #8 Change for a Dollar Game – Tony Gaddis – Starting Out With C++
Problem: -
Create a change-counting game that gets the user to enter the number of coins
required to make exactly one dollar. The program should ask the user to enter the
number of pennies, nickels, dimes, and quarters. If the total value of the coins entered
is equal to one dollar, the program should congratulate the user for winning the game.
Otherwise, the program should display a message indicating whether the amount
entered was more than or less than one dollar
Solution: -
#include <iostream>
using namespace std;
int main()
{
// Declare Constant and Assign Values
const float PENNIE = 0.01,
ONE_DOLLAR = 1,
NICKLE = 0.05,
DIMS = 0.10,
QUARTER = 0.25;
// Declare Variables For input & Total
float pennie, nickle, dims, quarter, total_coins = 0.0;
// Take Input From The User
cout << "How many pannies do you have: ";
cin >> pennie;
cout << "How many nickle do you have: ";
cin >> nickle;
cout << "How many dims do you have: ";
cin >> dims;
cout << "How many quarter do you have: ";
cin >> quarter;
// Calculations
pennie *= PENNIE;
nickle *= NICKLE;
dims *= DIMS;
quarter *= QUARTER;
total_coins = pennie + nickle + dims + quarter;
// Use Condition For Checking
if (total_coins == ONE_DOLLAR)
cout << "Congratulation You Win the Game";
else
cout << "Try Again";
// Return 0 To The Main Function
return 0;
}
This is the solution of this question
OUTPUT OF THIS QUESTION
Explanation of this Solution
Solution Explain With Comments in Solution.
Also, I attach CPP file of this problem. You can download this file
0 Comments