Write a program that tells what coins to give out for any amount of change from
ID: 3627240 • Letter: W
Question
Write a program that tells what coins to give out for any amount of change from 1 cent to 99 cents. For example, if the amount is 86 cents, the output would be something like the following:86 cents can be given as 3 quarters 1 dime and 1 penny
Use coin denominations of 25 cents (quarters), 10 cents (dimes), and 1 cent (pennies). Do not use nickel and half dollar coins. Your program will use the following function (among others):
void computeCoin(int coinValue, int& number, int& amountLeft);
For example, suppose the value of the variable amountleft is 86. Then, after the following call, the value of number will be 3 and the value of amountLeft will be 11 (because if oyu take three quarters from 86cents, that leaves 11 cents):
computecoins (25, number, amountLeft);
Include a loop that lets the user repeat this computation for new input values until the suer says he or she wants to end the program (Hint: use integer division and the % operator to implement this function.)
Explanation / Answer
#include<iostream>
using namespace std;
void computeCoin(int coinValue, int& number, int& amountLeft)
{
number = amountLeft / coinValue;
amountLeft = amountLeft % coinValue;
}
int main()
{
int cents;
int number;
char ch;
do
{
cout << "Enter no of cents. :";
cin >> cents;
while(cents<1 || cents >99)
{
cout << "Invalid cents entered. Re-Enter no of cents. :";
cin >> cents;
}
cout << cents <<" can be given as ";
computeCoin(25,number,cents);
if(number)
cout << number << " quarters ";
computeCoin(10,number,cents);
if(number)
cout << number << " dimes and ";
cout << cents << " penny"<<endl;
cout<< "do u want to continue enter y or n :";
cin >> ch;
}while(ch=='y');
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.