English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

-the digits are read from the keyboard.
-looking how to use the "if"statment or case statement .

anyhelp appreciated!

2007-03-28 03:18:15 · 1 answers · asked by zack g 1 in Computers & Internet Programming & Design

1 answers

The trick to this problem is using the modulus operator in your if statement. As you may know, modulus is an operator that will return the remainder of a division operation... for instance...

10 % 3 = 1

10 divided by 3 goes in 3 times with a remainder of 1.

Now you can use this idea to find out odd and even. A number that is mod with 2 and returns 0 is even, it will return 1 if it is odd.

3 % 2 = 1.... 3 is odd
2 % 2 = 0.... 2 is even
10 % 2 = 0...10 is even

So putting it together in an if statement...

if ((number %2) == 0) { System.out.println("Number is even"); }

Take this one step further with the switch...

int k = number % 2;

switch (k) {
case 0:
System.out.println( "Adding one to even" );
evennums++;
break;

case 1:
System.out.println("Adding one to odd");
oddnums++;
break;

default:
System.out.println("Not even or odd? How can this be?");
}

Hope the code above helps you out puts you in the right direction. Good luck with the rest of the program. Hope it turns out awesome!

2007-03-30 08:30:01 · answer #1 · answered by Martyr2 7 · 0 0

fedest.com, questions and answers