Back to track
#95 Easy Foundations

If / Else

Open the editor

Problem

Read an integer n and print "positive", "negative" or "zero".

Constraints

-10^9 <= n <= 10^9

Input

Line 1: an integer n.

Output

One word.

The idea

Concept

An if/else-if chain stops at the first true branch, so ordering the tests correctly is what makes the chain exhaustive without overlapping. Zero must be handled explicitly — it is neither positive nor negative.

Approach

Test > 0, then < 0, and let the final else cover zero.

Hints

  1. 1

    Only the first matching branch runs.

  2. 2

    Zero needs its own case.

  3. 3

    Match the words exactly, lower case.

Worked solution

Reference solution

Time: O(1) · Space: O(1)

One correct implementation. Any approach that satisfies the constraints is equally valid.

#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  long long n;
  if (!(cin >> n)) return 0;
  if (n > 0) cout << "positive" << '\n';
  else if (n < 0) cout << "negative" << '\n';
  else cout << "zero" << '\n';
  return 0;
}

© 2026 ChitChat