Next Even Number
Programming Basics · C++
TASK
Problem
Given an integer `n`, print the smallest even integer that is greater than or equal to `n`.
EXAMPLE
Example
1
2
LIMITS
Constraints
|n| ≤ 10^9.
LEARN
Theory for this problem
+
These exercises combine arithmetic, comparisons, and small branches. Keep each condition explicit and easy to verify.
Connection to “Next Even Number”: here, intermediate values are best kept in named variables so the formula stays readable and data types remain clear.
NEED HELP?
Hints
+
['Break the task into a few simple checks instead of compressing everything into a clever expression.', 'After solving “Next Even 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>
using namespace std;
int main() {
long long n;
cin >> n;
cout << (n%2==0?n+2:n+1) << '\n';
return 0;
}