You are here:

C++/Chinese Horoscope animal recognition algorithm

Advertisement


Question
I just want to ask if there is a simple algorithm in C++ to recognise under what animal horoscope a person is born in, when the user enters the year that they are born in.

There are 12 horoscope animals & pattern repeats every 12 years.

E.g. Rat = 1900, 1912, 1924, ....etc
    Ox = 1901, 1913, 1925......etc
    Tiger = 1902,1914, 1926....
    Rabbit = 1903,1915,1927.....
    .
    .
    .
    Dog = 1911,1923,1935....

I will appreciate any help, thx!

Answer
Hi,
 You can use the "mod of 12" idea. Using that,
Rat = 5
Ox = 6
Tiger = 7 and so on.

struct horo {
  char name [10];
};

struct horo myhoro[12];

int main ()
{
  int x;
  // here initialize the above array
  // Now get the input from user
 printf ("Enter the year\n");
 scanf ("%d", &x);
 printf ("Horo is %s\n", myhoro[x%12].name);
 return 0;
}

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Dharmender Rai

Expertise

I can answer general and system level C/C++ questions.

Experience

I have 5 years of experience in C++.

©2016 About.com. All rights reserved.