r/learnjava • • Oct 30 '16

What the HELL is a modulo?

I have a test tomorrow, in the test one of the questions will be to seperate a number from a 3 digit number, say I have 641, I need to know how to print out 6,4 and 1 seperately.

What the hell do I do ? The teacher is so bad I couldn't understand a word she said and neither did my class, we already complained about her but this isn't the issue, the issue is that I have no easy answers on the internet for what the modulo is.

Why does doing (n/100)%10; print out the hundred digit ? I have no idea how this works, please go easy on me.

0 Upvotes

31 comments sorted by

View all comments

1

u/Xian9 Oct 30 '16

That % operator basically gives you the remainder after dividing. So 102 % 10 would give you a remainder of 2. It's a bit different to the keyword "mod", more info here. Personally to get a single digit from a longer number I would cast it to a String and use the charAt() function.

-5

u/[deleted] Oct 30 '16

We "haven't learned it yet", I hate hate hate that. I find strings and charat to be sooo much easier than using the mod but noooooooo we can't advance to that till we learn the mod.