Temperature Category
Programming Basics · C++
TASK
Problem
Given an integer temperature, print `cold` if it is below 0, `cool` for 0–19, `warm` for 20–29, and `hot` for 30 or more.
EXAMPLE
Example
-5
cold
LIMITS
Constraints
-10^4 ≤ temperature ≤ 10^4.
LEARN
Theory for this problem
+
An `if` / `else if` / `else` chain checks alternatives from top to bottom. Put more specific or higher-priority conditions first.
Connection to “Temperature Category”: here, branching selects a case using a Boolean condition; test order matters when cases overlap.
NEED HELP?
Hints
+
['List the possible cases and make them mutually exclusive.', 'After solving “Temperature Category”, 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>
using namespace std;
int main() {
long long n;
cin >> n;
if (n < 0) cout << "cold";
else if (n < 20) cout << "cool";
else if (n < 30) cout << "warm";
else cout << "hot";
cout << '\n';
return 0;
}