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

  1. Use nested if-else for sign
  2. n % 2 for even/odd
  3. Check if sqrt(n)² equals n for perfect square

Related Control Flow exercises

Practice all Control Flow exercises · Run this idea in the Java compiler