Page 1 of 2 12 LastLast
Results 1 to 20 of 26
  1. #1
    batista11b5 is offline Member
    Join Date
    Oct 2010
    Posts
    23
    Rep Power
    0

    Default 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

  2. #2
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    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:

    Java Code:
    int remainder = 14 % 10;

    Remainder would hold the value of 4.
    Sincerely, Joshua Green
    Please REP if I help :)

  3. #3
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    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.
    Attached Thumbnails Attached Thumbnails How to calculate remainder in JAVA (%)-remainder.png  

  4. #4
    batista11b5 is offline Member
    Join Date
    Oct 2010
    Posts
    23
    Rep Power
    0

    Default

    thanks you guy so much , and how about

    36 % 8 = ?
    12 % 5 = ?

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    What value do you get when you divide 36 things into 8 piles? And what is the remainder, ie how many are left over?

  6. #6
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Sounds like a challenge! haha.
    Sincerely, Joshua Green
    Please REP if I help :)

  7. #7
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by batista11b5 View Post
    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!"

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  9. #9
    batista11b5 is offline Member
    Join Date
    Oct 2010
    Posts
    23
    Rep Power
    0

    Default

    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. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by batista11b5 View Post
    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?

  11. #11
    batista11b5 is offline Member
    Join Date
    Oct 2010
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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

  12. #12
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    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:
    Java 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.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by batista11b5 View Post
    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.

  14. #14
    batista11b5 is offline Member
    Join Date
    Oct 2010
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by Zack View Post
    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:
    Java 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

  15. #15
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by Fubarable View Post
    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...

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Zack View Post
    Oh wow, I completely missed that post. Whoooooops...
    Yep, and I missed Traveshein's post that was made even before that.

  17. #17
    batista11b5 is offline Member
    Join Date
    Oct 2010
    Posts
    23
    Rep Power
    0

    Default

    Hi ZAck ,following your answer : 26%6 will be 2 right

    +What if : 6 % 26 = ? please give me an answer , Thanks Zack again

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by batista11b5 View Post
    Hi ZAck ,following your answer : 26%6 will be 2 right
    Correct.

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

    please give me an answer
    We're here to help you learn, not to "give you answers".

  19. #19
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    (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?

  20. #20
    batista11b5 is offline Member
    Join Date
    Oct 2010
    Posts
    23
    Rep Power
    0

Page 1 of 2 12 LastLast

Similar Threads

  1. Program in Java To calculate GCD of n numbers.?
    By ankitsinghal_89 in forum New To Java
    Replies: 4
    Last Post: 02-15-2011, 10:23 AM
  2. how to discard remainder on division?
    By RobertF in forum New To Java
    Replies: 9
    Last Post: 03-13-2009, 01:20 PM
  3. [SOLVED] Modulus/Remainder results
    By antgaudi in forum New To Java
    Replies: 9
    Last Post: 10-13-2008, 04:49 AM
  4. BigInteger remainder results in zero
    By perito in forum New To Java
    Replies: 1
    Last Post: 03-21-2008, 05:07 PM
  5. Calculate Tax in java
    By toby in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 10:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •