Chessboard Color
Programming Basics · C++
TASK
Problem
Given chessboard coordinates `x` and `y` from 1 to 8, print `black` if the square is black and `white` otherwise. Square `(1,1)` is black.
EXAMPLE
Example
1 1
black
LIMITS
Constraints
1 ≤ x, y ≤ 8.
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 “Chessboard Color”: here, intermediate values are best kept in named variables so the formula stays readable and data types remain clear.
NEED HELP?
Hints
+
['List the possible cases and make them mutually exclusive.', 'After solving “Chessboard Color”, 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() {
int x, y;
cin >> x >> y;
cout << ((x + y) % 2 == 0 ? "black" : "white") << '\n';
return 0;
}