In: Computer Science
home / study / engineering / computer science / computer science questions and answers / write a program that in c++: 1.prompts the user to enter a positive integer, think of this ... Question: Write a program that in C++: 1.Prompts the user to enter a positive integer, think of this intege... Write a program that in C++: 1.Prompts the user to enter a positive integer, think of this integer as representing a specific number of pennies. 2. The program should calculate and display the least possible number of coins (quarters, dimes, nickels and cents) that may be used to represent the number of pennies entered by the user. For Example: If the user enters 28, then the program should display 1 quarter and 3 pennies. See other examples below. 3.The program MUST display the results using proper grammar (singular or plural). No global variables No labels or go to statements No infinite loops my professor wants me to unload a pdf and a txt file for the answer.
Answer:
#include <iostream>
using namespace std;
int main()
{
int QUARTERS = 25;
int DIMES = 10;
int NICKELS = 5;
int PENNIES = 1;
int change, numQuarters, numDimes, numNickels, numPennies;
cout << "Enter an amount : ";
cin >> change;
if(change > 0)
{
numQuarters = change / QUARTERS;
change -= numQuarters * QUARTERS;
numDimes = change / DIMES;
change -= numDimes * DIMES;
numNickels = change / NICKELS;
change -= numNickels * NICKELS;
numPennies = change / PENNIES;
change -= numPennies * PENNIES;
if(numQuarters != 0) cout << numQuarters << " Quarters ";
if(numDimes != 0) cout << numDimes << " Dimes ";
if(numNickels != 0) cout << numNickels << " Nickels ";
if(numPennies != 0) cout << numPennies << " Pennies ";
cout << endl;
}
else
cout << "Invalid amount" << endl;
return 0;
}