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 3233
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 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
|
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 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...
|
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...
| |
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,...
|
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.
|
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!
|
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, 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...
|
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...
| |
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...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |