Challenge: Even or Odd
Completed
Read a whole number and print whether it is even or odd.
Input
One whole number (it may be negative).
Output
<n> is even or <n> is odd
Example
Input
4
Output
4 is even
Editor
Java
Hints
A number is even when n % 2 == 0.
For negative odd numbers, n % 2 is -1, so test == 0, not == 1.
Solution explanation
In Java -3 % 2 is -1, so checking n % 2 == 0 works for every number.
Show solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if (n % 2 == 0) {
System.out.println(n + " is even");
} else {
System.out.println(n + " is odd");
}
}
} Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module