225k views
1 vote
Create a change-counting game that asks the user to enter what coins to use 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 or less than one dollar. Use constant variables to hold the coin values.

1 Answer

5 votes

Answer:

The cpp program is given below.

#include <stdio.h>

#include <iostream>

using namespace std;

int main()

{

//constant variables holding values of coins

const int penny=1;

const int nickel=5;

const int dime=10;

const int quarter=25;

//variables to hold user input of number of coins

int p, n, d, q, sum;

std::cout << "Enter the number of pennies: ";

cin>>p;

std::cout << "Enter the number of nickels: ";

cin>>n;

std::cout << "Enter the number of dimes: ";

cin>>d;

std::cout << "Enter the number of quarters: ";

cin>>q;

//total amount is computed

sum=penny*p;

sum=sum+(nickel*n);

sum=sum+(dime*d);

sum=sum+(quarter*q);

if(sum==100)

std::cout << "Congratulations! You won the game." << std::endl;

else

{

if(sum<100)

std::cout << "Your entered amount was less by " <<(100-sum)<< " cents. " <<std::endl;

else

std::cout << "Your entered amount was more by " <<(sum-100) << " cents. "<< std::endl;

}

return 0;

}

OUTPUT

Enter the number of pennies: 1

Enter the number of nickels: 1

Enter the number of dimes: 1

Enter the number of quarters: 1

Your entered amount was less by 59 cents.

Step-by-step explanation:

1. All the values of the coins, penny, nickel, dime and quarter are declared as constant integers.

2. The variables are declared to hold user input for the number of coins of all the constant denominations.

3. Once the user enters the required values, total sum of all the coins entered by the user is computed.

4. If the total value of the coins equals 100 cents, user won the game since the user entered correct values which summed up to 100 cents or 1 dollar.

5. If the total value of the coins does not equals 100 cents, the total is either less or more than 100 cents. The required difference between the user entered coins and 1 dollar is displayed to the user and the user loses the game.

6. The entire logic for user input is placed inside main() method.

7. All the variables and constants are also declared and defined respectively inside main() method.

User Joemfb
by
4.9k points