AlgoMaster Logo

Positive, Negative, or Zero

Last Updated: June 7, 2026

easy
2 min read

Understanding the Problem

Every integer is positive, negative, or zero. Report which one n is.

Zero is its own case. Asking "is n greater than 0?" and "is n less than 0?" leaves the case where both are false, which is when n is zero. Structure the checks so each runs only when the previous ones did not match, and the last branch absorbs whatever is left.

Key Constraints:

  • Three distinct outcomes → You need two comparisons, not one. After ruling out positive and negative, the only value remaining is zero.
  • Zero must be reported explicitly → Do not collapse zero into the negative or positive branch. Give it its own result so 0 returns "Zero".
  • n fits in 32 bits → A plain int holds the value with no overflow risk, and the comparisons are exact.

Approach 1: Else-If Ladder

Intuition

An else-if ladder picks one outcome from a small set of mutually exclusive options. Each test runs only when the earlier ones failed, and the final else is a catch-all.

Ask whether n is greater than 0. If not, ask whether it is less than 0. If neither holds, the value can only be zero, so the final branch returns "Zero" without another comparison.

Algorithm

  1. If n is greater than 0, return "Positive".
  2. Otherwise, if n is less than 0, return "Negative".
  3. Otherwise, the value must be 0, so return "Zero".

Example Walkthrough

Input:

0
n

The first test asks whether 0 is greater than 0, which is false. The second test asks whether 0 is less than 0, which is also false. With both comparisons ruled out, control reaches the final branch, and the answer is "Zero".

0
Z
1
e
2
r
3
o
output

Code