Correct Change I
You are given a certain number of nickels
and pennies
and a desired sum
of cents. Is it possible to select the exact change to reach that sum?
You will be given three numbers as input - nickels
, pennies
and sum
. Return true
if you can select coins to reach sum
exactly, and false
otherwise.
In this challenge, you can be given any number of nickels
but only 0 to 4 pennies
.
Image from Wikipedia
Note: A nickel is worth 5 cents. A penny is worth 1 cent.
Input/Output Format
Boilerplate code is provided for Java. For other languages, read in the numbers from the standard input in the following format.
- The input starts with N, the number of cases.
- N lines follow, which each contains 3 integers in order for the number of
nickels
, number ofpennies
, and the desiredsum
.
Print out true or false to the output for each case on its own line.
Click on "View Raw I/O" below to view sample input and output.
Challenge
Can you get the exact sum using the given number of nickels and pennies?
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Comments
Hongwen
Nov 3, 9:36 PMFor input set 5 3 19, we can get 19 using 3 pennies and 4 nickels. So it should be true there.
Hongwen
Nov 3, 9:39 PMSorry, I mistook nickels and pennies...
Amboss
Jan 7, 10:53 AM5, 4, 23 will result false !!!!!! it ca't be true 5*5 +4 = 24 ?????????????????
Learneroo
Jan 7, 11:16 AM@Amboss, the challenge is to see if you can select the exact change from the coins you are given. So if you have 5 nickels, you can select 4 of them, add 4 pennies for a sum of 24 cents.
Кирилл Варивода
Jan 7, 3:00 PMI've made the code, but when I run table below shows that all cases are correct EXCEPT that "Your Output" is incorrect, it's red.
I don't know why, I checked some made up cases manually on Eclipse.
Learneroo
Jan 7, 3:09 PMКирилл, When given 7 nickels and 3 pennies, can you get 35 cents? Yes, you just select the nickels and ignore the pennies. Check your code to see what it returns.
Кирилл Варивода
Jan 7, 4:04 PMAdmin (?), I've make some changes, even few, but still, there is something wrong. Your example works, thanks for it!)
I'm just curious where I get lost.
Learneroo
Jan 7, 4:12 PMКирилл. there's another example you're getting wrong. I think you should approach this problem a little differently. You can solve it with one if-else statement.
Кирилл Варивода
Jan 11, 2:12 PMDavid
Mar 6, 10:26 AMOMG :( this took me forever also my code is extensive.... looks like complying with DRY is going to take me looong time to master.
saeed
Mar 30, 12:27 PMAt the end of my program I write << return boolean; >> but program say error:
variable bool might not have been initialized
return bool;
saeed
Mar 30, 1:20 PMI found my solution;
when I defined boolean bool I must give amount to it LIKE boolean bool = false;
Learneroo
Jul 7, 11:28 AMI archived many comments here with solutions. You can link to your solution (in your submissions) to share it, but you shouldn't paste the whole solution in here.
Learneroo
Sep 3, 3:21 PM@mistermase nice. See also the featured answer
Shena
Jan 2, 9:39 AMi'm confused i'm not familiar with nickel and pennies and cents -.- do they have relationships with each other ? like how many cents are there in pennies or in nickel ? :)
Brayden Plush
May 4, 1:03 PMAmerican Currency^ LOL
Calvin
May 12, 11:13 AMFor those of you in different countries: Pennies are 1 cent, Nickles are 5 cents (each).
Learneroo
May 12, 11:16 AMAdded a note to main content on their value.
thales
Jul 15, 2:45 PMI think i have a good solution? is it a smart solution?
my code
Learneroo
Jul 15, 3:10 PMOK, but see the featured answers for a more concise answer.
Ravi
Jul 30, 6:38 AMI wrote my code in python. it's working if i run this in some other compiler but here it's not.
my code
Learneroo
Jul 30, 9:51 AMBoilerplate code for this challenge wasn't provided for Python, so you need to take in the input yourself from STDIN.
Lukas
Dec 1, 4:19 PMPrateek
May 12, 4:38 AMint a=sum/5;
int b=sum%5;
if(nickels>=a && pennies>=b){
return true;
}
return false;
Charlotte
Oct 19, 12:05 AMhow the heck did this pass all the tests..
Learneroo
Oct 19, 8:15 AMThanks, that's actually an edge case that wasn't covered before. The last test case will now check that condition so that code no longer passes.
Amanpreet Singh
Oct 30, 1:43 PMI did this way
if(nickels>=sum/5 && pennies>=sum%5)
{
return true;
}
else
{
return false;
}
Kyle Lemon
May 25, 8:42 AMHere's some working boilerplate for Ruby:
n = gets.chomp.to_i
n.times do
input = gets.chomp.split(" ").map(&:to_i)
nickels = input[0]
pennies = input[1]
desired_sum = input[2]
print correct_change?(nickels, pennies, desired_sum), "\n"
end
Fede
Sep 2, 3:58 AMhere goes my take!
if (sum % nickels <= pennies && ((nickels *5)+ pennies) >= sum){
return true;
}
else{
return false;
Soner Gönül
Mar 28, 2:57 PMHere is mine;
AMR
Aug 13, 6:45 AMI am stuck on this
Quick question - Can this exercise be done only with if statements?
Lang - Python
Appreciate the help
Thank you
Roger
Aug 17, 1:36 PMfunction recipe(sum ,nickels, pennies) {
let nickel = 5 * nickels / 100
let penny = 1 * pennies / 100
let change = nickel + penny
if (penny <= 4 && change % sum === 0) {
return 'true'
} else if (penny > 4) {
return 'false'
} else {
return 'false'
}
};