# How to calculate remainder in JAVA (%)

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-25-2010, 01:34 AM
batista11b5
How to calculate remainder in JAVA (%)
anyone explain for me :

15 % 5 = ?
36 % 8 = ?
14 % 10 = ?

please explain step by step to calculate ? Thanks so much
• 10-25-2010, 01:40 AM
joshdgreen
You simply take the first number: 15
And divide it by the second number: 5

15 / 5 = 3 with no remainder, so the modulus in this case would be 0.
14 / 10 = 1 with remainder of 4, so the modulus in this case would be 4.

So if you said:

Code:

`int remainder = 14 % 10;`

Remainder would hold the value of 4.
• 10-25-2010, 01:42 AM
travishein
Well, just like you would do long division by hand. The % or "modulus" operator means "perform a division operation, and take the remainder, if any". where the division is typically "integer division" meaning the part divided out is the even whole number, no fractional result.
• 10-25-2010, 02:47 AM
batista11b5
thanks you guy so much , and how about

36 % 8 = ?
12 % 5 = ?
• 10-25-2010, 02:50 AM
pbrockway2
What value do you get when you divide 36 things into 8 piles? And what is the remainder, ie how many are left over?
• 10-25-2010, 02:51 AM
joshdgreen
Sounds like a challenge! haha.
• 10-25-2010, 02:53 AM
Zack
Quote:

Originally Posted by batista11b5
thanks you guy so much , and how about

36 % 8 = ?
12 % 5 = ?

This sounds oddly like homework. Read the above posts as they explain the method perfectly. If you legitimately don't understand their meaning then post an actual question instead of just "solve this for me!"
• 10-25-2010, 03:15 AM
Fubarable
I second Zack's recommendation. Please at least show some effort.
• 10-25-2010, 03:16 AM
batista11b5
like you guy said :

12%5 =2.4 => remainder = 4 ,right ?

but in this question .

System.out.println (6.0 * 2 – 12 % 5 / 2) the correct result is : 11.0

but when i did like you guy just said :
12.0 - (4/2) => the result is : 10.0

=> so please explain it , I dont understand
• 10-25-2010, 03:23 AM
Fubarable
Quote:

Originally Posted by batista11b5
like you guy said :

12%5 =2.4 => remainder = 4 ,right ?

That's not how you calculate remainders. What's 2 * 5? Now can you use that number to figure out the remainder from 12%5?
• 10-25-2010, 03:26 AM
batista11b5
Quote:

Originally Posted by Fubarable
That's not how you calculate remainders. What's 2 * 5? Now can you use that number to figure out the remainder from 12%5?

please tell me the exact solution , I dont understand ,so that's why i ask you guy ,Thanks
• 10-25-2010, 03:28 AM
Zack
If you already know what a remainder is, ignore my post.

A remainder is the amount left after a number is taken out of another number a set amount of times. This is best explained by an example:
Code:

```Remainder of 26 / 6: 26 - 6 = 20         20 - 6 = 14                   14 - 6 = 8                           8 - 6 = 2         Since 2 is less than 6, it is the remainder. Therefore, the REMAINDER of 26/6 = 2.```
Modulus (A%B) calculates the remainder of A/B.
• 10-25-2010, 03:31 AM
Fubarable
Quote:

Originally Posted by batista11b5
please tell me the exact solution , I dont understand ,so that's why i ask you guy ,Thanks

Zack shows you above, but pbrockway already described how to do this much earlier in this thread.
• 10-25-2010, 03:32 AM
batista11b5
Quote:

Originally Posted by Zack
If you already know what a remainder is, ignore my post.

A remainder is the amount left after a number is taken out of another number a set amount of times. This is best explained by an example:
Code:

```Remainder of 26 / 6: 26 - 6 = 20         20 - 6 = 14                   14 - 6 = 8                           8 - 6 = 2         Since 2 is less than 6, it is the remainder. Therefore, the REMAINDER of 26/6 = 2.```
Modulus (A%B) calculates the remainder of A/B.

Thanks Zac so much , now i got it how it is , this's not assignment ,It just a review for my Exam next monday .Thanks again
• 10-25-2010, 03:57 AM
Zack
Quote:

Originally Posted by Fubarable
Zack shows you above, but pbrockway already described how to do this much earlier in this thread.

Oh wow, I completely missed that post. Whoooooops...
• 10-25-2010, 04:02 AM
Fubarable
Quote:

Originally Posted by Zack
Oh wow, I completely missed that post. Whoooooops...

Yep, and I missed Traveshein's post that was made even before that.
• 10-25-2010, 04:04 AM
batista11b5

+What if : 6 % 26 = ? please give me an answer , Thanks Zack again
• 10-25-2010, 04:11 AM
Fubarable
Quote:

Originally Posted by batista11b5

Correct.

Quote:

+What if : 6 % 26 = ?
Again, what do you think the answer should be, and why?

Quote:

• 10-25-2010, 04:19 AM
pbrockway2
(My previous reply was actually an edit of the snarky original.)

However I'll try again...

The % operator is all about left overs. a%b means simply "how many are left over when we attempt to put a things into b (equal) piles?". So, eg, if you put 26 things into 6 piles you find that you have 2 things left over. 26%6=2.

So what happens if you attempt to put 6 things into 26 equal piles? How many will be left over?
• 10-25-2010, 04:22 AM
batista11b5
is it 3 ? 26 / 6 = 4.3 => remainder = 3 , right ?
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last