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

3

u/g00glen00b Oct 30 '16

The modulo operator returns the "remainder" of a division. If you think back to your early days at school, when you first learned dividing using long divisions, you learned that for 14/4 the result is 3 and the remainder is 2. Because "4" fits "3" times in '14", but then you still have a remainder of "2" so that:

14 = 4 x 3 + 2

Now, why do they suggest using the modulo operator to get the different numbers (6, 4, 1). Well, let's start from the right. If we divide 641/10, we know that it fits 64 times inside 641 and the remainder is 1. In java terms this is:

int lastDigit = 641 % 10; // 1

Now, you can repeat the same process over and over again, however, we need to "shift" the number a bit. For example, if we want to know the second digit, we first divide 641 by 10, so that we only have 64. Now we can just retrieve the last digit again by using the modulo operator:

int secondDigit = 641 / 10 % 10; // = 64 % 10 = 4

Now, you should start to see a system here. You can keep doing this trick over and over again, but in stead of dividing by 10, you divide by 100, 1000, 10000, 100000, ... and you get the next digit every time. In your case, the left digit can be retrieved by using:

int firstDigit = 641 / 100 % 10; // = 6 % 10 = 6

In this case, it's pretty "useless" to use the modulo operator, because if you have a number with only 3 digits, dividing by 100 will already yield the left most digit. However, let's say that our number was 1234, then we would get:

int digit = 1234 / 100 % 10; // = 12 % 10 = 2

0

u/[deleted] Oct 30 '16

I'm telling you people say I understand computing fast, math is pretty mediocre to me but I can't, warp my head around this. If I don't understand the modulo right now will I have problems later in my programming career...??

21

u/mattibdtx Oct 30 '16

If you are unable to solve problems with the amount of help being offered to you in this thread, yes you will have problems later in your career.

4

u/joshuaherman Oct 31 '16

Lol this.

1

u/toilet_--gay_reddit Oct 31 '16

I felt kinda bad for him until I read some of his other comments.

3

u/g00glen00b Oct 30 '16

The modulo operator is a basic math operator and there's various information about it on the web. Being able to search for solutions by Googling and understanding them + basic math knowledge are things that can be useful to a developer.

However, there's no such thing as "the programming career", depending on the industry you'll be working in and what type of job, you might need to know more or less mathematics than others.

1

u/[deleted] Oct 30 '16

Modulo is the remainder from integer division.

4 / 3 = 1 r 1

4 % 3 = 1

1

u/[deleted] Oct 30 '16

What's a remainder ? English isn't my native lang.

2

u/[deleted] Oct 30 '16

It's the part that's left.

Consider that 4 / 3 = 1 r 1 and 4 -3 = 1.

Have you done the assigned reading?

-2

u/[deleted] Oct 30 '16

assigned reading

What's that

Didn't understand >Consider that 4 / 3 = 1 r 1 and 4 -3 = 1.

1

u/Rosydoodles Oct 30 '16

3*1 = 3, +1 = 4.

Therefore if you divide 4 by 3, you get one with a remainder (what you added above) of 1. The remainder is the result of modulo.

Assigned reading is the script from the class, powerpoint slides, or pages/chapters from the book the professor recommends.

1

u/[deleted] Oct 30 '16

It is when you are dealing with less than whole numbers, specifically by division. 4/3 = 1, because 4 divided by 3 goes in once (3), and has a remainder of 1 (which would be .25 decimally, or 1/4 fractionally).

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.

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

1

u/Blackheart595 Oct 30 '16

7 % 3 == 1. That's because 7 / 3 == 2 if you round the result down, but 2 * 3 == 6, so there's still 1 missing. Similarily, 18 % 7 == 4 because 18 == 2 * 7 + 4.

Actually, as you seem to have some difficulties with English, try reading these wikipedia articles here and here in your language, it will probably explain everything you need.

1

u/[deleted] Oct 30 '16

Nope, I speak near fluent level, it's just that I'm tired as of right now. The reason why I didn't understand modulo is because our teacher used it in finding out a certain digit in a tri-digit number but she didn't really expand on that and when I tried asking her for some help she just outright ignored me.

1

u/salvagestuff Oct 30 '16

When using modulo for finding out digits, it is pretty much all about divisions of 10.

In your example, (n/100)%10 what it does is bring the digit from the hundreds place to the end of the number. The modulo 10 then returns the very last digit.

Lets say that n = 6543

(6543/100)%10

The division of 100 gives you back 65 (remember int division drops the decimal places).

65%10 will return 5 because that is not divisible by 10 while anything in front of the 5 is divisible by 10.

If I wanted the 10's I would do (n/10)%10

(6543/10)%10 654%10 = 4

If I wanted the thousands it would be (n/1000)%10

(6543/1000)%10 6%10 = 6

If I wanted the last digit it would be n%10

If you wanted to print all numbers out it could look like this.

System.out.println("Thousands: " + (n/1000)%10 + " Hundreds:" + (n/100)%10 + " Tens:" + (n/10)%10 + " ones:"+ n%10);

I hope that makes sense.

1

u/[deleted] Oct 31 '16

(n/100)%10 gives you 6 in your example because (641/100) = 6. Remember, we're talking about integer division here. There are no decimals in integer division. Normally 641/100 would equal 6.41 but in integer division we use only whole numbers. So then you do 6/10 and keep only the remainder. 10 goes into 6 zero times with a remainder of 6. So 6 is your answer and that formula will always print out the digit of the hundred's place.

0

u/[deleted] Oct 30 '16

[deleted]

1

u/[deleted] Oct 30 '16

I understand it now, the proccess I mean, but the code not, can you please help me out here ?

int n = 741;
	    int t = n%10; // Yahidot
	    System.out.println("Number of Yahidot " + t);
	    int k = (n/10)%10; // Asarot
	    System.out.println("Number of Asarot " + k);
	    int j = n/100; // Meyot
	    System.out.println("Number of Meyot "+ j);

3

u/Sir_Joe Oct 31 '16 edited Oct 31 '16

Alllllright time to try to make an eli5 here.

Disclaimer : For most of you, these explanations may looks like they are tedious, boring, stupid and or something else (or even bad, I'm not gonna pretend that I'm the best guy to explain, I'm just gonna give it a go) . However, I know from experience (one of my closest friend had that problem) that sometime people aren't able to understand a concept that we consider 'basic' simply because they misunderstood (or just don't know) another concept that is required to understand the first one, and that confuse them. I think that's the case of the OP (but I may be wrong). That's why I will try to explain everything as I do not know what he misunderstood or simply didn't understood.


So let's get started :

First some definitions: Integer (int): any number that isn't a fraction; that doesn't include any '.' in it (7.4 isn't a integer).

Variable : You can consider variables like little boxes that contains something of a certain type. For example, you could consider, in the real life, that a shoes box is a variable that contain something of a certain type (shoes in that case but you could store others things in that box right ?). In java the idea is the same but your box (the variable) isn't usually containing shoes; instead they usually contain int (talked about them before) strings (basically text) and a lot of other things that you already know or will in the future. Also technically variables aren't containing the thing itself but instead contain a reference to where the object (or anything else) is in your ram but I guess that's for later.

System.out.println : Scary thing, isn't it :P For now, I would suggest you to simply see this as one of the million of tools Java gives you to program. This tool is pretty simple: You write System.out.println("put something in there") and whatever is in the parenthesis get printed on your console. A useful feature of that tool is that you can print multiples things with only one use. For example lets say I have this line System.out.println("Number of Yahidot " + 5); Java will understand that want you want to say is to print the string Number of Yahidot and the integer 5 in the console.

Modulo : From what I saw from you other comments, I think you understood that part but if you still want different explanation, I will be happy to help.

Comments (anything with // before) : Well there nothing to understand there. In java (and a lot of other programming languages) whenever you put this ' // ' in a line, the computer will ignore anything else past this sign in the same line. They are usually put this in the code in order to give some information to people that read the code on what it actually suppose to do (but you can really write anything you want; Once, one of my friend literally written a freaking poem in comments just for the heck of it in his code.).


Now let give a look of what currently does each lines:

1st : Declare the variable n (create a box named n) that contains the integer 741.

2nd : Declare another variable named t, but this time, instead of simply doing "this variable contains this number" like on the line one, you say this variable contains the result of the following operation : n%10 (741 % 10 in that case). After there is a comment that, I guess, simply mean that the variable t represent the Number of Yahidot.

3rd : This line say to java : print me the sentence "Number of Yahidot " and the value contained in t (in that case it should be 1) in my console.

4: Same thing like the second line but instead of containing the result of the n%10 operation, the variable k will contain the result of this operation: (n/10)%10

5: Well I can continue like that but I think you get the idea. However, don't be afraid to reply back if you feel you would like that I continue.


Finally, If I made any mistake in my explanation (I don't think I did but thats possible) I would like one of you guys tell it to me. I'm also a beginner in java (while perhaps more advanced that OP) and I will be happy to learn from my errors. Also I'm like OP not a native English speaker so please forgive my (sometime) bad English.


Also u/druskim don't give up. I know learning how to program can feel hard at first and you will probably have to accept that you cannot understand everything as long as your able to do it, but believe me, you will be proud later of what you've done and with how the world is headed, I think there is a bright and interesting future for programmers.

Have fun !

1

u/[deleted] Oct 31 '16

I know basically everything you said, just wanted to know why doing a module of 10 and other numbers results in x and y, but thank you so much for the detailed explanation

2

u/Sir_Joe Oct 31 '16 edited Oct 31 '16

I was pretty sure you did. But as I said in my comment, I don't know what you don't know and I wanted to be sure there wasn't anything else you wasn't sure about and do it in one comment. That may be a bit drastic but this way everything is in the same post :P