Sign of Number
Programming Basics · C++
TASK
Problem
Given an integer `n`, print `positive` if `n > 0`, `negative` if `n < 0`, and `zero` otherwise.
EXAMPLE
Example
-5
negative
LIMITS
Constraints
All integer magnitudes are at most 10^9; divisors are non-zero where used.
LEARN
Theory for this problem
+
`if`, comparison operators, and logical operators let a program choose output according to a condition.
Connection to “Sign of Number”: here, branching selects a case using a Boolean condition; test order matters when cases overlap.
NEED HELP?
Hints
+
['Write the condition directly and print exactly the required value.', 'After solving “Sign of Number”, verify the algorithm on your own small example and on an allowed boundary case. Print only the required result with no extra text.']
ANSWER
Solution
+
#include <iostream>
#include <algorithm>
#include <cstdlib>
using namespace std;
int main() {
long long n;
cin >> n;
if (n > 0) cout << "positive" << '\n';
else if (n < 0) cout << "negative" << '\n';
else cout << "zero" << '\n';
return 0;
}