473,513 Members | 2,409 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

monetary amounts in java

9 New Member
I have to write a program to prompt for a monetary amount from a user and then change that amount into 10 dollar bills, 5 dollar bills, one dollar bills, quarters, dimes, nickels, and pennies.
I wrote the program but sometimes it is one penny off. I have read some previous discussions about this which told me to use the BigDecimal class or swing classes but i havent learned about those yet in my class.I was wondering if there are any simpler solutions to the problem.
Feb 5 '07 #1
5 3220
r035198x
13,262 MVP
I have to write a program to prompt for a monetary amount from a user and then change that amount into 10 dollar bills, 5 dollar bills, one dollar bills, quarters, dimes, nickels, and pennies.
I wrote the program but sometimes it is one penny off. I have read some previous discussions about this which told me to use the BigDecimal class or swing classes but i havent learned about those yet in my class.I was wondering if there are any simpler solutions to the problem.
Post your code and we'll see if it cannot be modified to give the correct solution.
Feb 5 '07 #2
OCD
9 New Member
Expand|Select|Wrap|Line Numbers
  1. i
  2.  
  3. mport java.util.Scanner;
  4. import java.text.NumberFormat;
  5. import java.util.Locale;
  6.  
  7. public class Money3
  8. {
  9.                 public static void main(String args[])
  10.                 {
  11.                         NumberFormat fmt = NumberFormat.getCurrencyInstance  (Locale.US);
  12.                         Scanner input = new Scanner(System.in);
  13.  
  14.                         double amount;
  15.  
  16.                         int dollars;
  17.                         int cents;
  18.                         int tens;
  19.                         int fives;
  20.                         int ones;
  21.                         int quarters;
  22.                         int dimes;
  23.                         int nickels;
  24.                         int pennies;
  25.                         int money;
  26.  
  27.                         System.out.println("enter a monetary amount");
  28.                         amount = input.nextDouble();
  29.  
  30.                         dollars = (int) amount;
  31.                         cents = (int)(amount - dollars)*100;
  32.  
  33.  
  34.  
  35.                         tens = dollars/10;
  36.                         dollars = dollars%10;
  37.                         fives = dollars/5;
  38.                         dollars = fives%5;
  39.                         ones = dollars;
  40.  
  41.                         quarters = cents/25;
  42.                         cents = cents%25;
  43.                         dimes = cents/10;
  44.                         cents = cents%10;
  45.                         nickels = cents/5;
  46.                         cents = cents%5;
  47.                         pennies = cents;
  48.  
  49.  
  50.                         System.out.println(tens + " ten dollar bills");
  51.                         System.out.println(fives + " five dollar bills");
  52.                         System.out.println(ones + " one dollar bills");
  53.                         System.out.println(quarters + " quarters");
  54.                         System.out.println(dimes + " dimes");
  55.                         System.out.println(nickels + " nickels");
  56.                         System.out.println(pennies + " pennies");
  57.  
  58.         }
  59. }
  60.  
  61.  
  62.  
Feb 5 '07 #3
r035198x
13,262 MVP
Expand|Select|Wrap|Line Numbers
  1. i
  2.  
  3. mport java.util.Scanner;
  4. import java.text.NumberFormat;
  5. import java.util.Locale;
  6.  
  7. public class Money3
  8. {
  9. public static void main(String args[])
  10. {
  11. NumberFormat fmt = NumberFormat.getCurrencyInstance (Locale.US);
  12. Scanner input = new Scanner(System.in);
  13.  
  14. double amount;
  15.  
  16. int dollars;
  17. int cents;
  18. int tens;
  19. int fives;
  20. int ones;
  21. int quarters;
  22. int dimes;
  23. int nickels;
  24. int pennies;
  25. int money;
  26.  
  27. System.out.println("enter a monetary amount");
  28. amount = input.nextDouble();
  29.  
  30. dollars = (int) amount;
  31. cents = (int)(amount - dollars)*100;
  32.  
  33.  
  34.  
  35. tens = dollars/10;
  36. dollars = dollars%10;
  37. fives = dollars/5;
  38. dollars = fives%5;
  39. ones = dollars;
  40.  
  41. quarters = cents/25;
  42. cents = cents%25;
  43. dimes = cents/10;
  44. cents = cents%10;
  45. nickels = cents/5;
  46. cents = cents%5;
  47. pennies = cents;
  48.  
  49.  
  50. System.out.println(tens + " ten dollar bills");
  51. System.out.println(fives + " five dollar bills");
  52. System.out.println(ones + " one dollar bills");
  53. System.out.println(quarters + " quarters");
  54. System.out.println(dimes + " dimes");
  55. System.out.println(nickels + " nickels");
  56. System.out.println(pennies + " pennies");
  57.  
  58. }
  59. }
  60.  
  61.  
  62.  
Hey, is this the change problem where you have to give the least amount of coins?
Feb 5 '07 #4
OCD
9 New Member
you have to give the least amount of 10 dollar bills, 5 dollar bills, one dollar bills, quarters, nickels, dimes, and pennies not just coins
Feb 5 '07 #5
r035198x
13,262 MVP
you have to give the least amount of 10 dollar bills, 5 dollar bills, one dollar bills, quarters, nickels, dimes, and pennies not just coins
Yeah well it's just the same principle. This kind of problem was posted again last week and this was the initial attempt at it I posted. See if you can understand it.


Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.util.Scanner; //You have to import Scanner if you want to use it
  3. class One {
  4. public static void main(String[] args) {
  5.                 Scanner scanner = new Scanner(System.in);
  6. System.out.print("Enter the amount: ");
  7. double amount = scanner.nextDouble(); //Java is case sensitive so nextDouble() != nextdouble
  8. //You set the coins and/or notes denominations here
  9.                 // 1c     5c  10c  25c   50c  $1  $5 $10
  10. double[] coins = {0.01, 0.05, .1,  0.25, 0.5,  1,  5, 10}; //Put your denominations here
  11. double[] soln = new double[coins.length];
  12. double current = 0.0;
  13. int i = coins.length - 1;
  14. while(i >= 0) {
  15. double val = coins[i];
  16. while((val + current) <= amount) {
  17. current = current + val;
  18. soln[i] = soln[i] + coins[i];
  19. }
  20. i--;
  21. }
  22. System.out.println(current); // Check this value for amounts containing .1
  23. for(int j = 0;j < coins.length; j++) {
  24. System.out.println(coins[j] + " : " + soln[j] / coins[j]);
  25. }
  26.  
  27. }
  28. }
  29.  
  30.  
Feb 5 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
7218
by: Julia Baresch | last post by:
Hi everyone, I haven't found any reference to this problem on this group or in Access 97 help. I hope someone here knows this - probably a simple thing, but not obvious. I designed a query to...
14
9788
by: wane | last post by:
Hello, I have heard that one should avoid using float and double in monetary calculation because of the lack of preciseness. What is a good alternative? Thanks
2
2722
by: Rares Vernica | last post by:
Hi, Can I use locale to format monetary values? If yes, how? If no, is there something I can use? E.g., I have 10000 and I want to get "$10,000". Thanks, Ray
2
2340
by: java06 | last post by:
Hi I need to do a program that will prompt for and read a double value representing a monetary amount.Then determine the fewest numbers of each bill and coin needed to represent that amount, starting...
1
3333
by: mjkelly | last post by:
Hi, I have a stored procedure written in java in an Oracle 10g db. This sp takes a java.lang.String as input, creates a file on disk and writes the string contents to it and inserts the filename...
6
9211
by: Java1963 | last post by:
Need help with writting an application that prompt for and read a double value representing a monetary amount. -------------------------------------------------------------------------------- ...
7
10805
by: =?Utf-8?B?TW9iaWxlTWFu?= | last post by:
Hello everyone: I am looking for everyone's thoughts on moving large amounts (actually, not very large, but large enough that I'm throwing exceptions using the default configurations). We're...
0
1033
bugboy
by: bugboy | last post by:
Does anyone know a good methodology for calculating the monetary cost of a given query? Do you count average clock cycles over a large sampling then calculate server costs?... or is there some sort...
1
1206
by: weird0 | last post by:
Are there any monetary gains of doing MCPD? I believe there is tremendous addition to the amount of knowledge of an individual. Regards
0
7267
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7175
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7391
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7553
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7120
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4754
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3247
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3235
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.