# why does the modulus not help to find negative odd number

Page 1 of 1

## 11 Replies - 5581 Views - Last Post: 04 March 2010 - 06:12 PMRate Topic: 1 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=159591&amp;s=94bca6f02dec2e0570bbf52127942055&md5check=' + ipb.vars['secure_hash'], cur_rating: 1, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 brainy_creature

Reputation: 2
• 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?
Is This A Good Question/Topic? 0

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

### #2 japanir

• jaVanir

Reputation: 1013
• 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

### #3 pbl

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

Reputation: 8370
• Posts: 31,956
• 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);

### #4 brainy_creature

Reputation: 2
• 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

### #5 nick2price

• D.I.C Lover

Reputation: 565
• 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
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.

### #6 zim1985

• Grand Inquisitor

Reputation: 75
• 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.

### #7 brainy_creature

Reputation: 2
• 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

### #8 Gasper

Reputation: 13
• 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

### #9 zim1985

• Grand Inquisitor

Reputation: 75
• 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

brainy_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.

### #10 japanir

• jaVanir

Reputation: 1013
• 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.
-25 % 2 = -1 (negative)
-27 % 2 = -1 (negative)

when the mod is apply on two positive numbers, it will return a positive remains:
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.

### #11 pbl

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

Reputation: 8370
• Posts: 31,956
• 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
You waste people time replying on both

### #12 erik.price

• D.I.C Lover

Reputation: 486
• 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