Search⌘ K

Solution Review: Even or Odd

Explore how to apply Java's if-else conditional statements by writing a program that checks whether a number is even or odd. Understand the use of the modulus operator and control flow to set outcomes based on conditions.

Solution: is it even or odd? #

Java
class HelloWorld {
public static void main( String args[] ) {
int x = 3;
String answer;
if (x % 2 == 0) {
answer = "even";
}
else {
answer = "odd";
}
System.out.println(answer);
}
}

Understanding the Solution

Reasoning: An even number, when divided by 2, ...