Number Classification in Java: Explanation & Practice
Classify a number by multiple properties
Problem summary
Classify a number: print whether it's positive/negative/zero, even/odd, and if it's a perfect square.
Starter code
public class Main {
public static void main(String[] args) {
int n = 16; // Test case 1
// Print classification on separate lines
// Positive/Negative/Zero
// Even/Odd
// Perfect Square: Yes/No
}
}Expected output and test cases
- 16 is positive, even, perfect square
Positive Even Perfect Square: Yes
- -7 classification
Negative Odd Perfect Square: No
- 0 is even and 0² = 0
Zero Even Perfect Square: Yes
Hints
- Use nested if-else for sign
- n % 2 for even/odd
- Check if sqrt(n)² equals n for perfect square
Related Control Flow exercises
- Practice Neon Number Check in Java
- Practice Advanced Leap Year in Java
- Practice Number Pattern Spiral in Java
Practice all Control Flow exercises · Run this idea in the Java compiler