Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- //Declare Constants
- const double QUARTER=0.25; //value of a quarter
- const double DIME=0.10; //value of a dime
- const double NICKEL=0.05; //value of a nickel
- const double PENNY=0.01; //value of a penny
- const int DOLLAR=1; //value of a dollar bill
- const int FIVE=5; //value of a five dollar bill
- const int TEN=10; //value of a ten dollar bill
- const int TWENTY=20; //value of a twenty dollar bill
- int main(){
- //Declare Variables
- double purchase; //purchase amount
- double payment; //payment amount
- double total_change; //total change to be given
- int nickels; //total nickels
- int pennies; //total pennies
- int dimes; //total dimes
- int quarters; //total quarters
- int dollars; //total dollars
- int fives; //total five dollar bills
- int tens; //total ten dollar bills
- int twenty; //total twenty dollar bills
- //Ask for Purchase Amount
- cout<<"This program will compute the proper change from a purchase."<<endl;
- cout<<"\nPlease enter the purchase amount: $";
- cin>>purchase;
- //Check if the entered value is valid
- if (purchase<=0){
- cout<<"The purchase amount has an error!"<<endl;
- cout<<"Purchase amount must be at least 1 cent."<<endl;
- cout<<"Please enter the correct purchase amount:";
- cin>>purchase;
- }
- //Ask for Payment Amount
- cout<<"\nPlease enter the payment amount: $";
- cin>>payment;
- //Check if payment value is valid
- if (payment<purchase){
- cout<<"The payment amount has an error!"<<endl;
- cout<<"Payment amount must be greater than or equal to purchase amount"<<endl;
- cout<<"Please enter the correct payment amount:";
- cin>>payment;
- }
- //Calculate Change
- total_change=payment-purchase;
- if (total_change==0)
- {
- cout<<"No change required."<<endl;
- }
- else if(total_change<0.05)
- {
- pennies=total_change/PENNY;
- cout<<"\nThe change is:"<<endl;
- cout<<pennies<<" PENNIES"<<endl;
- }
- else if (total_change<0.10)
- {
- nickels=total_change/NICKEL;
- pennies=(total_change-(nickels*NICKEL))/PENNY;
- cout<<"\nThe change is:"<<endl;
- cout<<nickels<<" NICKEL(S)"<<endl;
- cout<<pennies<<" PENNIES"<<endl;
- }
- else if (total_change<0.25)
- {
- dimes=total_change/DIME;
- nickels=(total_change-(dimes*DIME))/NICKEL;
- pennies=(total_change-(dimes*DIME)-(nickels*NICKEL))/PENNY;
- cout<<"\nThe change is:"<<endl;
- cout<<dimes<<" DIME(S)"<<endl;
- cout<<nickels<<" NICKEL(S)"<<endl;
- cout<<pennies<<" PENNIES"<<endl;
- }
- else if (total_change<1)
- {
- quarters=total_change/QUARTER;
- dimes=(total_change-(quarters*QUARTER))/DIME;
- nickels=(total_change-(quarters*QUARTER)-(dimes*DIME))/NICKEL;
- pennies=(total_change-(quarters*QUARTER)-(dimes*DIME)-(nickels*NICKEL))/PENNY;
- cout<<"\nThe change is:"<<endl;
- cout<<quarters<<" QUARTER(S)"<<endl;
- cout<<dimes<<" DIME(S)"<<endl;
- cout<<nickels<<" NICKEL(S)"<<endl;
- cout<<pennies<<" PENNIES"<<endl;
- }
- else if (total_change<5)
- {
- dollars=total_change/DOLLAR;
- total_change=total_change-(dollars*DOLLAR);
- quarters=total_change/QUARTER;
- dimes=(total_change-(quarters*QUARTER))/DIME;
- nickels=(total_change-(quarters*QUARTER)-(dimes*DIME))/NICKEL;
- pennies=(total_change-(quarters*QUARTER)-(dimes*DIME)-(nickels*NICKEL))/PENNY;
- cout<<"\nThe change is:"<<endl;
- cout<<dollars<<" ONE(S):"<<endl;
- cout<<quarters<<" QUARTER(S)"<<endl;
- cout<<dimes<<" DIME(S)"<<endl;
- cout<<nickels<<" NICKEL(S)"<<endl;
- cout<<pennies<<" PENNIES"<<endl;
- }
- else if (total_change<10)
- {
- fives=total_change/FIVE;
- dollars=(total_change-(fives*FIVE))/DOLLAR;
- total_change=total_change-(fives*FIVE+dollars*DOLLAR);
- quarters=total_change/QUARTER;
- dimes=(total_change-(quarters*QUARTER))/DIME;
- nickels=(total_change-(quarters*QUARTER)-(dimes*DIME))/NICKEL;
- pennies=(total_change-(quarters*QUARTER)-(dimes*DIME)-(nickels*NICKEL))/PENNY;
- cout<<"\nThe change is:"<<endl;
- cout<<fives<<" FIVE(S)"<<endl;
- cout<<dollars<<" ONE(S):"<<endl;
- cout<<quarters<<" QUARTER(S)"<<endl;
- cout<<dimes<<" DIME(S)"<<endl;
- cout<<nickels<<" NICKEL(S)"<<endl;
- cout<<pennies<<" PENNIES"<<endl;
- }
- else if (total_change<20)
- {
- tens=total_change/TEN;
- fives=(total_change-(tens*TEN))/FIVE;
- dollars=(total_change-(fives*FIVE)-(tens*TEN))/DOLLAR;
- total_change=total_change-(tens*TEN+fives*FIVE+dollars*DOLLAR);
- quarters=total_change/QUARTER;
- dimes=(total_change-(quarters*QUARTER))/DIME;
- nickels=(total_change-(quarters*QUARTER)-(dimes*DIME))/NICKEL;
- pennies=(total_change-(quarters*QUARTER)-(dimes*DIME)-(nickels*NICKEL))/PENNY;
- cout<<"\nThe change is:"<<endl;
- cout<<tens<<" TEN(S)"<<endl;
- cout<<fives<<" FIVE(S)"<<endl;
- cout<<dollars<<" ONE(S):"<<endl;
- cout<<quarters<<" QUARTER(S)"<<endl;
- cout<<dimes<<" DIME(S)"<<endl;
- cout<<nickels<<" NICKEL(S)"<<endl;
- cout<<pennies<<" PENNIES"<<endl;
- }
- else if (total_change>=20)
- {
- twenty=total_change/TWENTY;
- tens=(total_change-(twenty*TWENTY))/TEN;
- fives=(total_change-(twenty*TWENTY)-(tens*TEN))/FIVE;
- dollars=(total_change-(twenty*TWENTY)-(fives*FIVE)-(tens*TEN))/DOLLAR;
- total_change=total_change-(twenty*TWENTY+tens*TEN+fives*FIVE+dollars*DOLLAR);
- quarters=total_change/QUARTER;
- dimes=(total_change-(quarters*QUARTER))/DIME;
- nickels=(total_change-(quarters*QUARTER)-(dimes*DIME))/NICKEL;
- pennies=(total_change-(quarters*QUARTER)-(dimes*DIME)-(nickels*NICKEL))/PENNY;
- cout<<"\nThe change is:"<<endl;
- cout<<twenty<<" TWENTIES"<<endl;
- cout<<tens<<" TEN(S)"<<endl;
- cout<<fives<<" FIVE(S)"<<endl;
- cout<<dollars<<" ONE(S):"<<endl;
- cout<<quarters<<" QUARTER(S)"<<endl;
- cout<<dimes<<" DIME(S)"<<endl;
- cout<<nickels<<" NICKEL(S)"<<endl;
- cout<<pennies<<" PENNIES"<<endl;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment