Results 1 to 4 of 4

Thread: Need Help

  1. #1
    roks is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Default 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
    ************************************************** **************

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    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));

  3. #3
    roks is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Default

    Hey Thanks curmudgeon...

    You are absolutely right... worked fine this time..

    Thanks

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by roks View Post
    Hey Thanks curmudgeon...
    You are absolutely right... worked fine this time..
    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,...)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •