❔ i need help finding how many 10s, 5s, 2s and 1s are in the number 18.
Im really having trouble finding the solution... i tried the division and modulo.. heres the code ive done :
//Goal: Write a program that asks the user for a cash amount and calculates the number of $10, $5, $2 and $1 bills that must be collected to obtain this amount.
//varialbes
int totalAmount;
int tenDollars;
int fiveDollars;
int twoDollars;
int oneDollars;
//variable for when i have the final result of 10s,5s,2s and 1s.
int resultTenDollars;
int resultFiveDollars;
int resultTwoDollars;
int resultOneDollars;
//contants
const int amountTenDollars = 10;
const int amountFiveDollars = 5;
const int amountTwoDollars = 2;
const int amountOneDollars = 1;
//person enters a value for the varible in the console
Console.WriteLine("Enter the total amount:");
totalAmount = int.Parse(Console.ReadLine());
//calcul ( this is where im not very sure what to do )
tenDollars = totalAmount/ amountTenDollars;
resultTenDollars = tenDollars; //1,8
fiveDollars = totalAmount % 5; //1,8
resultFiveDollars = fiveDollars;
twoDollars = totalAmount % 2; //1,8
resultTwoDollars = twoDollars;
oneDollars = totalAmount % 1; //0,8
//showing the result to the console
Console.WriteLine(tenDollars); //1
Console.WriteLine(fiveDollars); //3
Console.WriteLine(twoDollars); //0
Console.WriteLine(oneDollars); // 0
answer should be : 1 for 10s, 1 for 5s, 1 for2s and 1 for 5s ;/
84 Replies
How would you explain it to someone who needs to do it physically? To an actual worker at the cash register?
Maybe if you put it into words, it will be easier to put into code
^ that’s if you have a valid solution
if i have to give to someone 18 dollars in those amount,
ill start with doing 18 - 10 which is equal to 8 but how do i show the 1 on the console...
then ill do 8 minus 5 = 3, 3 minus 2 ( 1 ) and 1- 1 ( 0 ) = 1 ten, 1 five, 1 two and 1 one.
but im not sure how to show the ones on the console.. and if i use a bigger number ? i cant use the "-" sign..
wouldn't you rather use division instead of subtraction? there might be more than one ten, more than one five etc. in the number.
This should help:
- divide number by 10 to know how many 10s there are
- divide rest by 5 to know how many 5s there are
- divide rest of that by 2 to know how many 2s there are
- divide rest tof that by 1 to know how many 1s there are
now the question of how to show the ones on the console should be self explanatory, thats the numbers how many 10s, 5s, 2s, 1s we have, which would be stored in a variable and you show it as usual with
Console.WriteLine()
yea ive tried the division but it doesnt give the right answer :
18 / 10 = 1,8.
1,8(rest) / 5 = 0,36
0,36 (rest) / 2 = 0,18
0,18(rest) /1 = 0,18
answer should be : 1 ten, 1 five, 1 two, 1 one
the answer of the division is 1 ten, 0 five, 0 two, 0 one
with the modulos, i get the answer : 8 tens, 3 fives, 1 two, 0 one
18mod10 =8
8mod5 = 3
3mod2 = 1
1mod1 = 0
what do you mean by
1.8(rest)
?the total amount is 18, so when i divide it by 10 ( 10$), it gives me 1,8
thats not the rest thou
thats the result
the remainder of 18/10 is 8.
oh shit
isnt that modulos ?
you need to use both.
division is to see how many 10s etc you need
but then after that, you want to divide the rest
with the 8, how do i turn that in 1 in the console writeline ?
?
the answer of the modulo is 8, which means that there cant be anymore 10s going into the 18. that means that there only 1 10$ bill going in the 18. The problem is that it will show "8" on the console, not "1".
it should show 1, because theres only 1 10$bill that fits in 18$
is the code you pasted above still what you have?
because that will not work, ofc
heres the code im using rn :
doesnt work btw
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
If your code is too long, post it to: https://paste.mod.gg/dont just paste it. use the proper syntax
so its readable
wdym ?
I mean read what the bot wrote.
$codegif
this will actually be harder to do with that many variables
wouldn't you rather use a more appropriate datatype, like a dictionary?
much better.
like math class ?
?
no?
okok wdym by dictionnary
I mean a
Dictionary<int, int>
where the key would be the dollar amount, and the value would be the number of that value
ie 10:1
nevermind, we can do that later
anyways, lets understand the maths involved here a bit better shall we?
Given any starting sum, what is the first thing we do?yea alr
im not sure
really?
wdym what we do with a starting sum
your goal is to calculate how many 10s, 5s, 2s and 1s right?
yea
so.. describe the process
i would say, we divide it
say my starting sum is 28
good okay
we divide it
ok ok
why do we divide it? whats the result of the division?
and I dont mean the number, I mean what does it represent
ok so we divide it to see how many lets say 10s are in that number
okay, good
then what?
so if i divide 28 by 10, it give me 2,8. and this is where im stuck when i use the dividion. I would say, since there are 2 tens, i would take the 28, subtract it by 20 ( 2 tens ) = 8 and divide it by 5
what u think ?
kinda, but can we generalize that?
do we NEED to subtract the 20?
isnt there another mathematical operator we already discussed that does that for us?
modulo 😮 - then, we would take the 28, do modulo 10 =8 and then divide by 5 ?
right!
imma read the code gimme sec
var remainder = startingNumber % 10;
now here is where I come in and do the programmer thing
Can we generalize this?the whole thing or simply the mod ?
the whole thing
can you write out the code like I did above, but for both 10 and 5?
imma try it rn, gimme one sec
ok so
that what i did and the answer is now : 1 ten, 1 five, 1 two, 0 1s
show the relevant code.
Not quite?
lets read these out loud
the number of ten dollar bills is the total amount divided by 10. (so far so good)
resultTenDollars is the remainder (so far so good)
the number of five dollar bills is the resultTenDollars divided by 5 (so far so good)
resultkFiveDollars is the remainder of the total amount divided by 5 (???)
Hmm i see like i do the reminder of 28( total). What i should do instead, IS do thé reminder of thé reminder of ten ( resultTenDollars)
28mod10= 8,meaning that for the fives, i should do 8/5
Right ?
and to make this a lot nicer, why not keep the same variable for the remainder?
Yea, will be lot nicer
and now that we are at this step, do you perhaps see a pattern?
Yea its the same pattern of division and rest/ modulos
sure is
Thx alors 🙂
Alot*
we aint done here
:p
Fr ?
well yes
if you just copy the above section 4 times, you get ugly code
Hmm i see
thats why I suggested we use a different datatype
instead of storing the results in 4 variables, how about we store it in 1?
I think a
Dictionary<int,int>
would work really well here, as it makes it very clear what number is associated with what denominationLike storing them in one simple console.writeline ?
Oh ok ok
Im not sure what you mean by that tho
do you know what a dictionary is/does?
in C#, not in real life.
Are the ints, the numbers 10,5,2,1 ?
that would be the
key
here yesHmm alr alr
I havent seen it in my class yer
Yet*
its essentially a lookup table
it lets you associate a
key
with a value
Ok ill look it up
btw, is this for class?
in that case, you shold stop here.
Yea its for m'y class
if you show up to class with a bunch of code that, no offence, you clearly didn't write yourself..
it might not go so well
go with the 4 copypasted lines with values changed, its fine
Even if i dont consider it as * writing that myself* as well, i still think i learned alot from that.
sure
what I was going for was a method that takes in a list of denominations and a start value, and returns a dictionary of the correct change to give.
but that will be a bit too advanced for where you are currently
I dont think ill use the library tho, ill make sure to check it out tho
library?
you mean.. dictionary? 😄
Yea 😂😅
',' character is not allowed in the search, please try again. Query must match the following regex:
^[0-9A-Za-z.<>]$
https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2?view=net-7.0
here
its "full" name is
Dictionary<TKey,TValue>
and you can change TKey and TValue to be the types you need
so in our case, that was int for both, but you could replace either or bothalr ty so much 🙏
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.