11 Replies - 4251 Views - Last Post: 04 March 2010 - 06:12 PM Rate Topic: *---- 1 Votes

#1 brainy_creature  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 177
  • Joined: 07-August 06

why does the modulus not help to find negative odd number

Posted 03 March 2010 - 12:29 PM

for( int i= -100; i<=100 ; i++){
if((i%2)==1)
System.out.println(i);


why does this code in java yields +ve odd nos?
what is the concept here? :shuriken:
Is This A Good Question/Topic? 0
  • +

Replies To: why does the modulus not help to find negative odd number

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: why does the modulus not help to find negative odd number

Posted 03 March 2010 - 12:48 PM

the modulo operator, used on two int values (int num1,num2) that way:
num1 % num2
will return the remains of the division operation of num1 / num2.
lets have some examples:
suppose you do that operation:
12 % 5 = 2
12 / 5 = 2 and remains of 2
13 % 6 = 1
13 / 6 = 2 and remains of 1
10 % 2 = 0
10 / 2 = 5 and remains of 0.

using the mod operator in the way you describes in your code, will find the odd numbers, because every even number will divide in 2 with NO remains.
2 / 2 = 1 and 0 remains. (i.e 2 % 2 = 0)
8 / 2 = 4 and 0 remains. (i.e 8 % 2 = 0)
22 / 2 = 11 and 0 remains. (i.e 22 % 2 = 0)
i.e (evenValue) % 2 = 0 for every even value.

However, every odd value will leave a remians of 1 when divided by 2.
3 / 2 = 1 and 1 remains (i.e 3 % 2 =1)
5 / 2 = 2 and 1 remians (i.e 5 % 2 = 1)
19 / 2 = 9 and 1 remains. (i.e 19 % 2 = 1)

so in your loop, every number between 0 and 100 which will leave a remain of 1 when divided by 2 is odd.
negative numbers will leave a remains of -1.

This post has been edited by japanir: 03 March 2010 - 12:49 PM

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: why does the modulus not help to find negative odd number

Posted 03 March 2010 - 08:02 PM

Better to do:

if((i%2) != 0)
System.out.println(i);
Was This Post Helpful? 1
  • +
  • -

#4 brainy_creature  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 177
  • Joined: 07-August 06

Re: why does the modulus not help to find negative odd number

Posted 04 March 2010 - 12:38 AM

public class className{
public static void main(String[] args){
for(int i = -100; i<=100; i++){
if ((i%2)==1))
System.out.println(i);
}
}
}


why doesnt this print negative odd numbers ?
what is the concept associated with modulus in java, can anybody please explain

This post has been edited by brainy_creature: 04 March 2010 - 03:40 AM

Was This Post Helpful? 0
  • +
  • -

#5 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: why does the modulus not help to find negative odd number

Posted 04 March 2010 - 12:55 AM

What exactly do you mean it doesnt work with negative numbers? I am no mathematician, but a modulus is the remainder of a division. So if you do 3 divided by 2, You know it goes once, and leaves remainder one. So the modulus is one (I really do hop I am correct, my maths is a bit rusty :rolleyes2:
I cant see what your trying to do with your code. Firstly, I am not even sure this will enter the loop
for(int i = 100; i<=100; i++){

However, it might do. All depends if it lets it enter once. However, if you get to
if ((i%2)==1))

i will always be 100, as you never change this value. So, 100 mod 2 = 0, which means it will never enter the if statement.
Was This Post Helpful? 0
  • +
  • -

#6 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: why does the modulus not help to find negative odd number

Posted 04 March 2010 - 02:32 AM

nick2price is correct. The % in Java calculates the remainder of a division problem. For example:

8 % 3 = 2
Explanation: 3 can go into 8 twice, leaving the remainder as 2.

8 % 2 = 0
Explanation: 2 goes into 8 evenly. No remainder.

As for your loop, I think what you were trying to accomplish is:
for(int i = 1; i <= 100; i++)
{
    if(i % 2 == 0)
        System.out.println(i);
}


This, as opposed to what you have, will cycle from 1 to 100 and print out all the numbers that are multiples of 2. Because your loop started at 100 and ended at 100, you never printed anything - the loop ended before it started.

Hope this clears it up for you.
Was This Post Helpful? 0
  • +
  • -

#7 brainy_creature  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 177
  • Joined: 07-August 06

Re: why does the modulus not help to find negative odd number

Posted 04 March 2010 - 03:43 AM

sorry guys it was a typo
the for loop should iterate from -100 to 100
this would give only positive odd numbers!!
but why is my question
Was This Post Helpful? -1
  • +
  • -

#8 Gasper  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 66
  • Joined: 29-May 09

Re: why does the modulus not help to find negative odd number

Posted 04 March 2010 - 05:33 AM

I am not sure whether division remainder and modulo are same thing.
However (strange) result you get is because of implementation.

How negative numbers division remainder is solved check
http://en.wikipedia....odulo_operation
(chart on right)

And remainder
http://en.wikipedia.org/wiki/Remainder
Was This Post Helpful? 1
  • +
  • -

#9 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: why does the modulus not help to find negative odd number

Posted 04 March 2010 - 06:39 AM

View Postbrainy_creature, on 04 March 2010 - 01:43 AM, said:

sorry guys it was a typo
the for loop should iterate from -100 to 100
this would give only positive odd numbers!!
but why is my question

You can't have a negative remainder.
8 & -3 = 2
Explanation: Modulus returns what would become a decimal, these decimals can't be negative.
8 / -3 = -2.666666667, but because you are using modulus, instead of getting -2 - 2/3, you just get a 2.
Was This Post Helpful? 0
  • +
  • -

#10 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: why does the modulus not help to find negative odd number

Posted 04 March 2010 - 06:51 AM

when you operate the mod operator between a negative and positive number will return a negative remains.
in your case
-25 % 2 = -1 (negative)
-27 % 2 = -1 (negative)

when the mod is apply on two positive numbers, it will return a positive remains:
in your case:
99 % 2 = 1
97 % 2 = 1

as you can see, executing the mod on 2, all negative numbers will return a negative remains (-1), while all positive will return a positive remains 1. and you print only numbers which return a positive remain:
if ((i%2)==1)) 

which means, only the positive numbers will be printed.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: why does the modulus not help to find negative odd number

Posted 04 March 2010 - 05:30 PM

Avoid double topics :crazy:
You waste people time replying on both
Was This Post Helpful? 0
  • +
  • -

#12 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: why does the modulus not help to find negative odd number

Posted 04 March 2010 - 06:12 PM

Following japanir's logic, you could use Math.abs(), so the result would work for positive and negative numbers:
if (Math.abs(i%2) == 1))


You could also go with what pbl mentioned earlier in the thread
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1