473,378 Members | 1,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Coins

mia023
89
Hi i have an assignment that i don't understand and need help in.

Write a program that reads in a command line integer N(number of pennies) and prints out the best way(fewest number of coins) to make change using US coins (quarters, dimes, nickels and pennies only). Your algorithm should dispense as many quarters as possible, then dimes, then nickels, and finally pennies.
Oct 29 '07 #1
12 2509
JosAH
11,448 Expert 8TB
Hi i have an assignment that i don't understand and need help in.

Write a program that reads in a command line integer N(number of pennies) and prints out the best way(fewest number of coins) to make change using US coins (quarters, dimes, nickels and pennies only). Your algorithm should dispense as many quarters as possible, then dimes, then nickels, and finally pennies.
Here's a little hint: suppose you've got 81 pennies; watch this:

81/25 == 3 (quarters)
81%25 == 6 (rest of the change)

etc. etc.

kind regards,

Jos
Oct 29 '07 #2
mia023
89
why did you do 81%25 and how can i get the rest
Oct 29 '07 #3
JosAH
11,448 Expert 8TB
why did you do 81%25 and how can i get the rest
The % is operator is Java's (and C and C++'s) modulo operator. 81%25 == 6
because the rest is 6 when you divide 81 by 25.

kind regards,

Jos
Oct 29 '07 #4
mia023
89
should i use nested loops or if statements
Oct 29 '07 #5
Ganon11
3,652 Expert 2GB
I don't think you'll need either - just repeat Jos' set of commands for quarters, dimes, and nickels, and whatever is left is pennies.
Oct 29 '07 #6
mia023
89
this is my code but i think it is wrong


public class Coin{
public static void main(String[]args){
int N=Integer.parseInt(args[0]);

int q=N%25;
int d=N%10;
int n=N%5;

int p=q+d+n;

System.out.println(N/25+" quarters");
System.out.println(N/10+" dimes");
System.out.println(N/5+" nickels");
System.out.println(p+" pennies");
}
}
Oct 29 '07 #7
mia023
89
sorry i send the code 2 times it was by mistake
Oct 29 '07 #8
JosAH
11,448 Expert 8TB
sorry i send the code 2 times it was by mistake
No need to worry, I removed one of them; and indeed your code is completely wrong.
Back to the example again: for 81 pennies you can fit 81/25 == 3 quarters and
you have 81%25 == 6 pennies left. Now generalize this and don't sprinkle in
the / and % operators where they don't make sense. Do your math.

kind regards,

Jos
Oct 29 '07 #9
mia023
89
No need to worry, I removed one of them; and indeed your code is completely wrong.
Back to the example again: for 81 pennies you can fit 81/25 == 3 quarters and
you have 81%25 == 6 pennies left. Now generalize this and don't sprinkle in
the / and % operators where they don't make sense. Do your math.

kind regards,

Jos
should i loop them and for example generalize the formula by N%num where N is the number of pennies and num is how many pennies are worth in dimes,nickels and quarters
Oct 30 '07 #10
r035198x
13,262 8TB
should i loop them and for example generalize the formula by N%num where N is the number of pennies and num is how many pennies are worth in dimes,nickels and quarters
Indeed you should generalize it.
Oct 30 '07 #11
mia023
89
Indeed you should generalize it.
Can you please tell me the conversions(pennies to quarters,etc....) and tell me what is wrong with the code above.
Oct 30 '07 #12
r035198x
13,262 8TB
Can you please tell me the conversions(pennies to quarters,etc....) and tell me what is wrong with the code above.
I don't know what you mean by conversions (pennies to quarters ...), I've never even seen any of them ...

What you need to do is have your algorithm (pseudo code) down.
Store the denominations in, say an array. Biggest coing on position 0, second biggest on position 1, e.t.c and smallest coin on last posistion in the array.
The algorithm should be very easy to construct from there using the greedy algorithm.
Oct 30 '07 #13

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

Similar topics

3
by: Shark1 | last post by:
Hello. I'm suppose to write a program that will match the price given by the user using the least number of coins. There are 5 different type of coins. I get this output, the count is...
22
by: Ben Finney | last post by:
Howdy all, I've recently packaged 'enum' in PyPI. In its description, I make the claim that it creates "immutable" enumeration objects, and that the enumeration values are "constant" values. ...
0
by: wilson_20004 | last post by:
A Division of Cash Cards International, Ltd Saturday, February 11, 2006 The GoldLynks Club Dedicated to the Advancement and Enrichment of all our Members Man has treasured Gold and Silver...
2
by: sharritta | last post by:
Could you help me design an interface that will find the number of quarters, dimes, nickels, and pennies from an amount entered by the user. I really need the code for the amount textbox and the...
9
by: smartbei | last post by:
Hello, I am a newbie with python, though I am having a lot of fun using it. Here is one of the excersizes I am trying to complete: the program is supposed to find the coin combination so that with...
5
by: OCD | last post by:
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...
1
by: MsBarnes | last post by:
I'm a newbie to the programming field and I really need help to understand what i'm doing wrong. This is the exercise i'm working on I need to create an application that allows the users to enter...
26
by: jmartmem | last post by:
Greetings, I have an ASP page with two dynamic dependent list boxes written in JavaScript. My dependent lists work great, but my problem is that the values for "Program_Name" and "Project_Name"...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.