OCD 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.
5 3220
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.
OCD 9
New Member - i
-
-
mport java.util.Scanner;
-
import java.text.NumberFormat;
-
import java.util.Locale;
-
-
public class Money3
-
{
-
public static void main(String args[])
-
{
-
NumberFormat fmt = NumberFormat.getCurrencyInstance (Locale.US);
-
Scanner input = new Scanner(System.in);
-
-
double amount;
-
-
int dollars;
-
int cents;
-
int tens;
-
int fives;
-
int ones;
-
int quarters;
-
int dimes;
-
int nickels;
-
int pennies;
-
int money;
-
-
System.out.println("enter a monetary amount");
-
amount = input.nextDouble();
-
-
dollars = (int) amount;
-
cents = (int)(amount - dollars)*100;
-
-
-
-
tens = dollars/10;
-
dollars = dollars%10;
-
fives = dollars/5;
-
dollars = fives%5;
-
ones = dollars;
-
-
quarters = cents/25;
-
cents = cents%25;
-
dimes = cents/10;
-
cents = cents%10;
-
nickels = cents/5;
-
cents = cents%5;
-
pennies = cents;
-
-
-
System.out.println(tens + " ten dollar bills");
-
System.out.println(fives + " five dollar bills");
-
System.out.println(ones + " one dollar bills");
-
System.out.println(quarters + " quarters");
-
System.out.println(dimes + " dimes");
-
System.out.println(nickels + " nickels");
-
System.out.println(pennies + " pennies");
-
-
}
-
}
-
-
-
- i
-
-
mport java.util.Scanner;
-
import java.text.NumberFormat;
-
import java.util.Locale;
-
-
public class Money3
-
{
-
public static void main(String args[])
-
{
-
NumberFormat fmt = NumberFormat.getCurrencyInstance (Locale.US);
-
Scanner input = new Scanner(System.in);
-
-
double amount;
-
-
int dollars;
-
int cents;
-
int tens;
-
int fives;
-
int ones;
-
int quarters;
-
int dimes;
-
int nickels;
-
int pennies;
-
int money;
-
-
System.out.println("enter a monetary amount");
-
amount = input.nextDouble();
-
-
dollars = (int) amount;
-
cents = (int)(amount - dollars)*100;
-
-
-
-
tens = dollars/10;
-
dollars = dollars%10;
-
fives = dollars/5;
-
dollars = fives%5;
-
ones = dollars;
-
-
quarters = cents/25;
-
cents = cents%25;
-
dimes = cents/10;
-
cents = cents%10;
-
nickels = cents/5;
-
cents = cents%5;
-
pennies = cents;
-
-
-
System.out.println(tens + " ten dollar bills");
-
System.out.println(fives + " five dollar bills");
-
System.out.println(ones + " one dollar bills");
-
System.out.println(quarters + " quarters");
-
System.out.println(dimes + " dimes");
-
System.out.println(nickels + " nickels");
-
System.out.println(pennies + " pennies");
-
-
}
-
}
-
-
-
Hey, is this the change problem where you have to give the least amount of coins?
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
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. -
-
import java.util.Scanner; //You have to import Scanner if you want to use it
-
class One {
-
public static void main(String[] args) {
-
Scanner scanner = new Scanner(System.in);
-
System.out.print("Enter the amount: ");
-
double amount = scanner.nextDouble(); //Java is case sensitive so nextDouble() != nextdouble
-
//You set the coins and/or notes denominations here
-
// 1c 5c 10c 25c 50c $1 $5 $10
-
double[] coins = {0.01, 0.05, .1, 0.25, 0.5, 1, 5, 10}; //Put your denominations here
-
double[] soln = new double[coins.length];
-
double current = 0.0;
-
int i = coins.length - 1;
-
while(i >= 0) {
-
double val = coins[i];
-
while((val + current) <= amount) {
-
current = current + val;
-
soln[i] = soln[i] + coins[i];
-
}
-
i--;
-
}
-
System.out.println(current); // Check this value for amounts containing .1
-
for(int j = 0;j < coins.length; j++) {
-
System.out.println(coins[j] + " : " + soln[j] / coins[j]);
-
}
-
-
}
-
}
-
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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
|
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
|
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...
|
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...
| |
by: Java1963 |
last post by:
Need help with writting an application that prompt for and read a double value representing a monetary amount.
--------------------------------------------------------------------------------
...
|
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...
|
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...
|
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
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |