In: Computer Science
Using C++
Giving change. Implement a program that directs a cashier how to give change. The program has two inputs: the amount due and the amount received from the customer. Compute the difference, and compute the dollars, quarters, dimes, nickels, and pennies that the customer should receive in return.
Please find the required python script:
#=======================================================================
#include <iostream>
using namespace std;
int main() {
float amount_due, amount_rec;
int total_cents,remaining_cents,dollars=0, quarters=0, dimes=0, nickels=0, pennies=0;
cout<<"Amount due: ";
cin>>amount_due;
cout<<"Amount received: ";
cin>>amount_rec;
while(amount_due>amount_rec)
{
cout<<"Insufficient amount received! Re-enter: ";
cin>>amount_rec;
}
if(amount_due==amount_rec)
{
cout<<"Perfect amount received, No change due!!";
}
else{
total_cents = (amount_rec*1000-amount_due*1000)/10; // This will convert the due amaount to cents
dollars = (total_cents/100);
remaining_cents = total_cents - dollars*100;
quarters = (remaining_cents/25);
remaining_cents = remaining_cents - quarters*25;
dimes = (remaining_cents/10);
remaining_cents = remaining_cents - dimes*10;
nickels = (remaining_cents/5);
remaining_cents = remaining_cents - nickels*5;
pennies = (remaining_cents/1);
cout<<"\nChange to give:\n";
cout<<"Dollars: "<<dollars<<", Quarters: "<<quarters<<", Dimes: "<<dimes<<", Nickels: "<<nickels<<", Pennies: "<<pennies<<"\n";
}
}
#========================================================================
Note: Please mind the indentation in the script! A reference image of the script is being given at the last.
Sample run:
Script image:
Hope tis helps!
**************** PLEASE THUMBS UP!!!!!!!!!!! ****************