Results 1 to 10 of 10
- 10-12-2008, 10:46 AM #1
[SOLVED] Modulus/Remainder results
What would be the result of longVariable?
int intVariable1 = 9;
int intVariable2 = 10;
longVariable = intVariable1 + 1 % intVariable2;
I would guess it would be 0 since 10 goes into 10 once and nothing remains...the correct answer is 10 and I have NO idea why...
What's your guess? :o"The minute you settle for less than you deserve, you get even less than you settled for." Maureen Dowd
- 10-12-2008, 11:03 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
No it's 10, I mean the result is 10 not 0 as you expect.
It's all about operators presidencies in Java. The % operator has higher precedence than + operator. So in your code,
evaluates first. That is 1. And then it's added to 9, result is 10. Hope it's clear to you.Java Code:1 % intVariable2
- 10-12-2008, 02:35 PM #3
If you have a compiler and a jvm, the fastest and most accruate method to find the answer is to use them.What would be the result
intVariable1 + 1 % intVariable2
is the same as
intVariable1 + (1 % intVariable2)
% has higher precedence than +Last edited by Norm; 10-12-2008 at 02:59 PM.
- 10-12-2008, 02:53 PM #4
Member
- Join Date
- Oct 2008
- Location
- UK
- Posts
- 65
- Rep Power
- 0
well
1%n is always 1 for n>1
reason is... if you divide 1 by a number (unless that number is also 1) you get an answer of zero, and a remainder of 1.
So your sum is just 1+9=10
- 10-12-2008, 05:03 PM #5
how is the remainder 1 if the answer is 0?
nothing remains...?
why do u not get 1 when it's 1%1?
I'm so confused...and I heard this is 3rd grade stuff... :-P
thanks for the feedback. :)"The minute you settle for less than you deserve, you get even less than you settled for." Maureen Dowd
- 10-12-2008, 07:59 PM #6
Member
- Join Date
- Oct 2008
- Location
- UK
- Posts
- 65
- Rep Power
- 0
You need to understand modular arithmetic, I would just google it. The % symbol is referred to as mod a lot of the time.
Let me give you some examples:
24%4 is 0 since
4x6+0 = 24
23%4 is 3 since
4x5+3 = 23
1%4 is 1 since
4x0+1 = 1
Hope that helps you more than it hinders
- 10-12-2008, 08:24 PM #7
According to Sun...
According to Sun's JSL the rules for the Remainder Operator (%) are:
Expressions
- 10-12-2008, 11:22 PM #8
Oh my goodness, Paul, you have really shed some great light on this!
You put it in such a colorful, visual, way!
Thanks!"The minute you settle for less than you deserve, you get even less than you settled for." Maureen Dowd
- 10-12-2008, 11:28 PM #9
Thanks everybody!
I can move on to chapter 2 now. :)"The minute you settle for less than you deserve, you get even less than you settled for." Maureen Dowd
- 10-13-2008, 03:49 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
how to take query results to a jlist!!
By themburu in forum JDBCReplies: 1Last Post: 06-07-2008, 10:51 AM -
how to take query results to a jlist!!
By themburu in forum New To JavaReplies: 3Last Post: 06-07-2008, 10:45 AM -
date and calender not getting the right results
By valoyivd in forum New To JavaReplies: 4Last Post: 04-14-2008, 11:51 AM -
BigInteger remainder results in zero
By perito in forum New To JavaReplies: 1Last Post: 03-21-2008, 04:07 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks