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/desrtfx Oct 30 '16 edited Oct 30 '16

Have you learned to divide numbers in school? I mean, before you learned about decimal numbers?

Example:

  • The teacher has 17 apples and should distribute these 17 apples evenly across his 5 pupils. How many whole apples does each child get? How many does the teacher have left?

Now: 5 goes in 17 three times with 2 apples left.

Each child gets three apples and the teacher still has two more left.

The 3 apples each child gets is the result of the division, the 2 apples left are the remainder.

The remainder can also be calculated by modulo. 17 modulo 5 will result in 2.

You will need to learn to google such questions - best in your native language. Programming and learning to program requires strong google skills.

-1

u/[deleted] Oct 30 '16

Obviously I learned division, we just never touched remainders I guess.

I did google, and did not understand shit.

6

u/dougie0341 Oct 30 '16

Doing division without remainders is pretty much impossible

0

u/[deleted] Oct 30 '16

I'm telling you never in my life have I used remainders, or maybe we're talking about different stuff ?

are you talking about let's say, 7 / 2, that results in 3.5, are we talking about the 3**.5** or are we talking about 7 mod 2 ?

1

u/dougie0341 Oct 30 '16

7/2 is 3 with a remainder of 1

7 mod 2 is 1

Do you see how that works?

2

u/[deleted] Oct 30 '16

Yes because 3 fits into 7 2 times and then 1 is left.

Can you see my reply to the other guy about me not understanding how to use module to extract a number?

10

u/dougie0341 Oct 30 '16

You just explained how modulos work though....

2

u/g00glen00b Oct 30 '16

Because, if you divide by 10, the remainder is always the last digit and the result of dividing by 10 is the original number without the last digit.

  • 1234 / 10 = 123 (remainder = 4)

  • 99 / 10 = 9 (remainder = 9)

  • 987654321 / 10 = 98765432 (remainder = 1)

  • ...

You should start to see a pattern here right? The remainder of a modulo by 10 is always the last digit of the number. So in that case you actually extracted the last digit.

1

u/SmaugTheMagnificent Oct 31 '16

Well then better start now dude