157k views
3 votes
Write a program with total change amount as an integer input that outputs the change using the fewest coins, one coin type per line. The coin types are dollars, quarters, dimes, nickels, and pennies. Use singular and plural coin names as appropriate, like 1 penny vs. 2 pennies. Ex: If the input is: 0 or less, the output is: no change Ex: If the input is: 45 the output is: 1 quarter 2 dimes

User Karoid
by
4.4k points

1 Answer

3 votes

Answer:.

// Program is written in C++.

// Comments are used for explanatory purposes

// Program starts here..

#include<iostream>

using namespace std;

int main()

{

// Declare Variables

int amount, dollar, quarter, dime, nickel, penny;

// Prompt user for input

cout<<"Amount: ";

cin>>amount;

// Check if input is less than 1

if(amount<=0)

{

cout<<"No Change";

}

else

{

// Convert amount to various coins

dollar = amount/100;

amount = amount%100;

quarter = amount/25;

amount = amount%25;

dime = amount/10;

amount = amount%10;

nickel = amount/5;

penny = amount%5;

// Print results

if(dollar>=1)

{

if(dollar == 1)

{

cout<<dollar<<" dollar\\";

}

else

{

cout<<dollar<<" dollars\\";

}

}

if(quarter>=1)

{

if(quarter== 1)

{

cout<<quarter<<" quarter\\";

}

else

{

cout<<quarter<<" quarters\\";

}

}

if(dime>=1)

{

if(dime == 1)

{

cout<<dime<<" dime\\";

}

else

{

cout<<dime<<" dimes\\";

}

}

if(nickel>=1)

{

if(nickel == 1)

{

cout<<nickel<<" nickel\\";

}

else

{

cout<<nickel<<" nickels\\";

}

}

if(penny>=1)

{

if(penny == 1)

{

cout<<penny<<" penny\\";

}

else

{

cout<<penny<<" pennies\\";

}

}

}

return 0;

}

User Tiziano
by
4.2k points