Results 1 to 4 of 4
Thread: Need Help
- 05-27-2010, 08:21 PM #1
Member
- Join Date
- May 2010
- Posts
- 2
- Rep Power
- 0
Need Help
Hi
When I am running below Java program, It does not gives me desired output.
Am wondering what is wrong here?
************************************************** **************
public static void check(){
System.out.println((10 % 100) == 0);
if((10 % 100) == 0){
System.out.println("it is even number");
} else{
System.out.println("this is odd number");
}
}
************************************************** **************
OUTPUT
false
this is odd number
************************************************** **************
- 05-27-2010, 08:23 PM #2
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
Hello, and welcome to the java-forums.
In the past when I've tested for "even-ness", I have usually mod'ed the number by 2, not 100:
Java Code:int myNumber = 10; System.out.println("myNumber is even?: " + ((myNumber % 2) == 0));
- 05-27-2010, 08:32 PM #3
Member
- Join Date
- May 2010
- Posts
- 2
- Rep Power
- 0
Hey Thanks curmudgeon...
You are absolutely right... worked fine this time..
Thanks
- 05-27-2010, 08:45 PM #4
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
You're welcome, but more importantly, do you understand why this is so? For a number to be even, it must be evenly divisible by 2, which is the same as saying that the number mod 2 is 0. If the number mod 100 is 0, then the number is a multiple of 100 (i.e., 200, 500, 10323200,...)


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks