Guest User

Untitled

a guest
Jan 19th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. //Declare Constants
  5.  
  6. const double QUARTER=0.25; //value of a quarter
  7. const double DIME=0.10; //value of a dime
  8. const double NICKEL=0.05; //value of a nickel
  9. const double PENNY=0.01; //value of a penny
  10. const int DOLLAR=1; //value of a dollar bill
  11. const int FIVE=5; //value of a five dollar bill
  12. const int TEN=10; //value of a ten dollar bill
  13. const int TWENTY=20; //value of a twenty dollar bill
  14.  
  15. int main(){
  16.  
  17. //Declare Variables
  18.  
  19. double purchase; //purchase amount
  20. double payment; //payment amount
  21. double total_change; //total change to be given
  22. int nickels; //total nickels
  23. int pennies; //total pennies
  24. int dimes; //total dimes
  25. int quarters; //total quarters
  26. int dollars; //total dollars
  27. int fives; //total five dollar bills
  28. int tens; //total ten dollar bills
  29. int twenty; //total twenty dollar bills
  30.  
  31. //Ask for Purchase Amount
  32.  
  33. cout<<"This program will compute the proper change from a purchase."<<endl;
  34. cout<<"\nPlease enter the purchase amount: $";
  35. cin>>purchase;
  36.  
  37. //Check if the entered value is valid
  38.  
  39. if (purchase<=0){
  40. cout<<"The purchase amount has an error!"<<endl;
  41. cout<<"Purchase amount must be at least 1 cent."<<endl;
  42. cout<<"Please enter the correct purchase amount:";
  43. cin>>purchase;
  44. }
  45.  
  46. //Ask for Payment Amount
  47.  
  48. cout<<"\nPlease enter the payment amount: $";
  49. cin>>payment;
  50.  
  51. //Check if payment value is valid
  52.  
  53. if (payment<purchase){
  54. cout<<"The payment amount has an error!"<<endl;
  55. cout<<"Payment amount must be greater than or equal to purchase amount"<<endl;
  56. cout<<"Please enter the correct payment amount:";
  57. cin>>payment;
  58. }
  59.  
  60. //Calculate Change
  61. total_change=payment-purchase;
  62.  
  63. if (total_change==0)
  64. {
  65. cout<<"No change required."<<endl;
  66. }
  67. else if(total_change<0.05)
  68. {
  69. pennies=total_change/PENNY;
  70. cout<<"\nThe change is:"<<endl;
  71. cout<<pennies<<" PENNIES"<<endl;
  72. }
  73. else if (total_change<0.10)
  74. {
  75. nickels=total_change/NICKEL;
  76. pennies=(total_change-(nickels*NICKEL))/PENNY;
  77. cout<<"\nThe change is:"<<endl;
  78. cout<<nickels<<" NICKEL(S)"<<endl;
  79. cout<<pennies<<" PENNIES"<<endl;
  80. }
  81. else if (total_change<0.25)
  82. {
  83. dimes=total_change/DIME;
  84. nickels=(total_change-(dimes*DIME))/NICKEL;
  85. pennies=(total_change-(dimes*DIME)-(nickels*NICKEL))/PENNY;
  86. cout<<"\nThe change is:"<<endl;
  87. cout<<dimes<<" DIME(S)"<<endl;
  88. cout<<nickels<<" NICKEL(S)"<<endl;
  89. cout<<pennies<<" PENNIES"<<endl;
  90. }
  91. else if (total_change<1)
  92. {
  93. quarters=total_change/QUARTER;
  94. dimes=(total_change-(quarters*QUARTER))/DIME;
  95. nickels=(total_change-(quarters*QUARTER)-(dimes*DIME))/NICKEL;
  96. pennies=(total_change-(quarters*QUARTER)-(dimes*DIME)-(nickels*NICKEL))/PENNY;
  97. cout<<"\nThe change is:"<<endl;
  98. cout<<quarters<<" QUARTER(S)"<<endl;
  99. cout<<dimes<<" DIME(S)"<<endl;
  100. cout<<nickels<<" NICKEL(S)"<<endl;
  101. cout<<pennies<<" PENNIES"<<endl;
  102. }
  103. else if (total_change<5)
  104. {
  105. dollars=total_change/DOLLAR;
  106. total_change=total_change-(dollars*DOLLAR);
  107. quarters=total_change/QUARTER;
  108. dimes=(total_change-(quarters*QUARTER))/DIME;
  109. nickels=(total_change-(quarters*QUARTER)-(dimes*DIME))/NICKEL;
  110. pennies=(total_change-(quarters*QUARTER)-(dimes*DIME)-(nickels*NICKEL))/PENNY;
  111. cout<<"\nThe change is:"<<endl;
  112. cout<<dollars<<" ONE(S):"<<endl;
  113. cout<<quarters<<" QUARTER(S)"<<endl;
  114. cout<<dimes<<" DIME(S)"<<endl;
  115. cout<<nickels<<" NICKEL(S)"<<endl;
  116. cout<<pennies<<" PENNIES"<<endl;
  117. }
  118. else if (total_change<10)
  119. {
  120. fives=total_change/FIVE;
  121. dollars=(total_change-(fives*FIVE))/DOLLAR;
  122. total_change=total_change-(fives*FIVE+dollars*DOLLAR);
  123. quarters=total_change/QUARTER;
  124. dimes=(total_change-(quarters*QUARTER))/DIME;
  125. nickels=(total_change-(quarters*QUARTER)-(dimes*DIME))/NICKEL;
  126. pennies=(total_change-(quarters*QUARTER)-(dimes*DIME)-(nickels*NICKEL))/PENNY;
  127. cout<<"\nThe change is:"<<endl;
  128. cout<<fives<<" FIVE(S)"<<endl;
  129. cout<<dollars<<" ONE(S):"<<endl;
  130. cout<<quarters<<" QUARTER(S)"<<endl;
  131. cout<<dimes<<" DIME(S)"<<endl;
  132. cout<<nickels<<" NICKEL(S)"<<endl;
  133. cout<<pennies<<" PENNIES"<<endl;
  134. }
  135. else if (total_change<20)
  136. {
  137. tens=total_change/TEN;
  138. fives=(total_change-(tens*TEN))/FIVE;
  139. dollars=(total_change-(fives*FIVE)-(tens*TEN))/DOLLAR;
  140. total_change=total_change-(tens*TEN+fives*FIVE+dollars*DOLLAR);
  141. quarters=total_change/QUARTER;
  142. dimes=(total_change-(quarters*QUARTER))/DIME;
  143. nickels=(total_change-(quarters*QUARTER)-(dimes*DIME))/NICKEL;
  144. pennies=(total_change-(quarters*QUARTER)-(dimes*DIME)-(nickels*NICKEL))/PENNY;
  145. cout<<"\nThe change is:"<<endl;
  146. cout<<tens<<" TEN(S)"<<endl;
  147. cout<<fives<<" FIVE(S)"<<endl;
  148. cout<<dollars<<" ONE(S):"<<endl;
  149. cout<<quarters<<" QUARTER(S)"<<endl;
  150. cout<<dimes<<" DIME(S)"<<endl;
  151. cout<<nickels<<" NICKEL(S)"<<endl;
  152. cout<<pennies<<" PENNIES"<<endl;
  153.  
  154. }
  155. else if (total_change>=20)
  156. {
  157. twenty=total_change/TWENTY;
  158. tens=(total_change-(twenty*TWENTY))/TEN;
  159. fives=(total_change-(twenty*TWENTY)-(tens*TEN))/FIVE;
  160. dollars=(total_change-(twenty*TWENTY)-(fives*FIVE)-(tens*TEN))/DOLLAR;
  161. total_change=total_change-(twenty*TWENTY+tens*TEN+fives*FIVE+dollars*DOLLAR);
  162. quarters=total_change/QUARTER;
  163. dimes=(total_change-(quarters*QUARTER))/DIME;
  164. nickels=(total_change-(quarters*QUARTER)-(dimes*DIME))/NICKEL;
  165. pennies=(total_change-(quarters*QUARTER)-(dimes*DIME)-(nickels*NICKEL))/PENNY;
  166. cout<<"\nThe change is:"<<endl;
  167. cout<<twenty<<" TWENTIES"<<endl;
  168. cout<<tens<<" TEN(S)"<<endl;
  169. cout<<fives<<" FIVE(S)"<<endl;
  170. cout<<dollars<<" ONE(S):"<<endl;
  171. cout<<quarters<<" QUARTER(S)"<<endl;
  172. cout<<dimes<<" DIME(S)"<<endl;
  173. cout<<nickels<<" NICKEL(S)"<<endl;
  174. cout<<pennies<<" PENNIES"<<endl;
  175. }
  176.  
  177. return 0;
  178. }
Add Comment
Please, Sign In to add comment