[Solved] Ancient Japanese calendar exercise in C++


According to your list the Color rotates in a 10 years cycle and then has always 2 subsequent years with same color. Given that rule you can calculate an index in a field of year colors.

#include <vector>
#include <string>
#include <iostream>

using namespace std;

int main()
{
    const int base_year = 1984;
    vector<string> colors = { "green", "red", "yellow", "white", "black" };

    int year;
    cout << "Insert year: ";
    cin >> year;

    int offset = year - base_year;
    int index = (offset % 10) / 2;
    if (index < 0) index += colors.size();

    cout << "year offset " << offset << " color index :" << "year color: " << colors[index] << endl;

    return 0;
}

solved Ancient Japanese calendar exercise in C++