Home >>Cpp Programs >CPP Program to determine the colour of chess square
In this example, we will see a C++ program through which we can determine the colour of the chess square.
A chess board is equally divided into 64 identical squares in which there are 32 black and 32 white squares alternately.
Program:
#include <iostream.h>
#include <cctype>
using namespace std;
int main()
{
char string[10], x;
cout << "Enter the coordinates of the square, \
\nthe first coordinate A to H and second coordinate 1 to 8: ";
cin.getline(string, 10);
x = string[0];
x = tolower(x);
string[0] = x;
if (string[0] == 'a' || string[0] == 'c' || string[0] == 'e' || string[0] == 'g')
{
if (string[1] == '1' || string[1] == '3' || string[1] == '5' || string[1] == '7')
cout << "Black square";
else
cout << "White square";
}
else
{
if (string[1] == '1' || string[1] == '3' || string[1] == '5' || string[1] == '7')
cout << "white square";
else
cout << "Black square";
}
return 0;
}