473,587 Members | 2,542 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 3233
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
7239
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 pull payment data from my database for accounting. It has 3 tables with one-to-many joins: Table1 --> one-to-many --> Table2 --> one-to-many --> Table3 The payment amount field is in Table3. I entered a few negative
14
9798
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
2725
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
2341
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 with the highest (maximum size needed is a ten dollar bill). For example if the value entered is 47.63 then the program should print: 4 ten dollar bills 1 five dollar bills 2 one dollar bills 2 quarters 1 dimes 0 nickels 3 pennies Thanks...
1
3343
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 (plus some other data) in a table in the database and returns the newly generated primary key (as a java.lang.Integer). The call spec defines the input parameter as a LONG. I have also written a java client app to test this and it works fine up to a...
6
9220
by: Java1963 | last post by:
Need help with writting an application that prompt for and read a double value representing a monetary amount. -------------------------------------------------------------------------------- Hello all, I am new to this Java stuff and, I need help in writting an application 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,...
7
10813
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 doing a proof-of-concept on WCF whereby we have a Windows form client and a Server. Our server is a middle-tier that interfaces with our SQL 05 database server.
0
1040
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 of ballpark method? I'm trying to avoid trial and error methods. Any thoughts, articles or first hand experience would really help. Thanks!
1
1211
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
7927
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8220
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8222
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6632
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5723
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5396
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2367
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1457
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.