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?

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?

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.

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

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

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

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

However, it might do. All depends if it lets it enter once. However, if you get to

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.

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.

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:

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.

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.

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

the for loop should iterate from -100 to 100

this would give only positive odd numbers!!

but why is my question

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

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

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

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.

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:

which means, only the**positive numbers will be printed**.

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

if ((i%2)==1))

which means, only the

Posted 04 March 2010 - 06:12 PM

Page 1 of 1

- Caffeine Lounge
- Corner Cubicle
- Student Campus
- Software Development
- Industry News
- Introduce Yourself
- Nightmare.In.Code

- C and C++
- VB.NET
- Java
- C#
- ASP.NET
- .NET Framework
- VB6
- PHP
- Mobile Development
- Python
- Ruby
- Game Development
- Databases
- ColdFusion
- Assembly
- Other Languages
- 52 Weeks Of Code

- Web Development
- HTML & CSS
- JavaScript
- Graphic Design
- Flash & ActionScript
- Blogging
- SEO & Advertising
- Web Servers & Hosting
- Site Check

- Book Review: Murach's Beginning Java with NetBeans
- Graph Data Structure Tutorial
- Swing, Passive Model-View-Presenter in 5 minutes.
- Book Review: Murach's Java Servlets and JSP
- Phobos - A JavaFX Games Engine: Part 2 - JavaFX Scene API and the FSM
- Maven Tutorial 2 - Adding Dependencies
- Maven Tutorial 1 - Installation and Getting Started
- Phobos - A JavaFX Games Engine: Part 1 - Intro to Threading and DP
- Swing to JavaFX
- Swing, Top-Down 2
**219 More Java Tutorials...**

- C Snippets
- C++ Snippets
- Java Snippets
- Visual Basic Snippets
- C# Snippets
- VB.NET Snippets
- ASP.NET Snippets
- PHP Snippets
- Python Snippets
- Ruby Snippets
- ColdFusion Snippets
- SQL Snippets
- Assembly Snippets
- Functional Programming Snippets
- Perl Snippets
- HTML/CSS Snippets
- Javascript Snippets
- Flash/ActionScript Snippets
- ASP Snippets
- Linux, Unix, and Bash Snippets
- Other Languages Snippets
- Regex

Copyright 2001-2015 **MediaGroup1 LLC**, All Rights Reserved

A**MediaGroup1 LLC** Production - Version 6.0.2.1.36

Server: secure3

A

Server: secure3