easyProgramming Fundamentals
If-Else Statements
Classify an integer as positive, negative, or zero.
Work through the cases
3 examplesInput notation
One integer.Expected result
positive, negative, or zero in lowercase.Case 1
Input
-8
Expected output
negative
Case 2
Input
0
Expected output
zero
Case 3
Input
12
Expected output
positive
These notes use simplified input/output notation. The linked platform defines the method signature and judge format.
FROM IDEA TO IMPLEMENTATION
Understand the approach.
Compare the documented approaches below. Brute force, better, and optimal versions appear where available; a direct exercise may need only one approach.
Check the sign, treating zero separately. Time and extra space: O(1).
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
ArrayList<Long> values = new ArrayList<>();
while (in.hasNextLong()) values.add(in.nextLong());
long[] a = values.stream().mapToLong(Long::longValue).toArray();
long n = a[0];
System.out.println(n > 0 ? "positive" : n < 0 ? "negative" : "zero");
}
}
Reference code. Platform solutions may use supplied node types, imports, or collection libraries; foundational examples are complete programs.