Chinese zodiac: Difference between revisions

m
no edit summary
(Chinese zodiac in BASIC256)
mNo edit summary
 
(74 intermediate revisions by 33 users not shown)
Line 1:
 
{{task}}Determine the Chinese zodiac sign and related associations for a given year.
 
Traditionally,In the Chinese have countedcalendar, years are identified using two simultaneouslists of cycleslabels, one of length 10 (the "celestial" stems(or "heavenly") "stems" – and one of length 12 (the "terrestrial" branches(or "earthly"); the"branches". combinationThe resultslabels indo anot repeatingreally 60-yearhave pattern.any Mappingmeaning theoutside branchestheir topositions twelvein traditionalthe animaltwo deitieslists; resultsthey're insimply thea well-knowntraditional "Chineseenumeration zodiac"device, assigningused eachmuch yearas toWesterners ause givenletters animaland numbers. ForThey example,were Saturday,historically Januaryused 28,for 2017months CEand (indays theas commonwell Gregorianas calendar)years, beginsand the lunisolarstems yearare ofstill thesometimes used for school Roostergrades.
 
Years cycle through both lists concurrently, so that both stem and branch advance each year; if we used Roman letters for the stems and numbers for the branches, consecutive years would be labeled A1, B2, C3, etc. Since the two lists are different lengths, they cycle back to their beginning at different points: after J10 we get A11, and then after B12 we get C1. However, since both lists are of even length, only like-parity pairs occur (A1, A3, A5, but not A2, A4, A6), so only half of the 120 possible pairs are included in the sequence. The result is a repeating 60-year pattern within which each name pair occurs only once.
 
Mapping the branches to twelve traditional animal deities results in the well-known "Chinese zodiac", assigning each year to a given animal. For example, Saturday, February 10, 2024 CE (in the common Gregorian calendar) began the lunisolar Year of the Dragon.
 
The celestial stems do not have noa one-to-one mapping like that of the branches to animals; however, the five <i>pairs</i> of consecutive stems are each belongassociated towith one of the five traditional Chinese<i>wǔxíng</i> elements (Wood, Fire, Earth, Metal, and Water). Further, one of the two years within each element's governance is associatedassigned withto <i>yin</i>, the other withto <i>yang</i>.
 
Thus, 2017the Chinese year beginning in 2024 CE is also the yinyang year of FireWood. Note that sinceSince 12 is an even number, the association between animals and yin/yang aspect doesn't change.; Consecutiveconsecutive Years of the RoosterDragon will cycle through the five elements, but will always be yin, despite the apparent conceptual mismatch between the male animals and the female aspectyang.
 
;Task: Create a subroutine or program that will return or output the animal, yin/yang association, and element for the lunisolar year that begins in a given CE year.
Line 14 ⟶ 17:
;Requisite information:
* The animal cycle runs in this order: Rat, Ox, Tiger, Rabbit, Dragon, Snake, Horse, Goat, Monkey, Rooster, Dog, Pig.
* The element cycle runs in this order: Wood, Fire, Earth, Metal, Water.
* Each element gets two consecutive years; a yang followed by a yin.
* The yang year precedes the yin year within each element.
* The currentfirst 60-year cycle(Wood beganRat, inyang) 1984 CE;of the firstcurrent 60-year cycle of the Common Era began in 41984 CE.
 
Thus,The 1984 was thelunisolar year ofbeginning thein Wood2024 Rat (yang), 1985 was the year of the Wood Ox (yin), and 1986 the year of the Fire Tiger (yang); 2017CE - which, as already noted, is the year of the FireWood RoosterDragon (yinyang) - is the 34th year41st of the current cycle.
 
;Information for optional task:
Line 24 ⟶ 27:
* The twelve terrestrial branches are '''子''' ''zĭ'', '''丑''' ''chŏu'', '''寅''' ''yín'', '''卯''' ''măo'', '''辰''' ''chén'', '''巳''' ''sì'', '''午''' ''wŭ'', '''未''' ''wèi'', '''申''' ''shēn'', '''酉''' ''yŏu'', '''戌''' ''xū'', '''亥''' ''hài''. In ASCII Pinyin, those are "zi3", "chou3", "yin2", "mao3", "chen2", "si4", "wu3", "wei4", "shen1", "you3", "xu1", and "hai4".
 
Therefore 1984 was '''甲子''' (''jiă-zĭ'', or jia3-zi3)., while 20172024 is the 34th year of the current cycle, '''丁酉甲辰''' (''dīngjĭa-yŏuchén'' or ding1jia3-you3chen2).
=={{header|11l}}==
{{trans|C++}}
 
<syntaxhighlight lang="11l">V animals = [‘Rat’, ‘Ox’, ‘Tiger’, ‘Rabbit’, ‘Dragon’, ‘Snake’, ‘Horse’, ‘Goat’, ‘Monkey’, ‘Rooster’, ‘Dog’, ‘Pig’]
V elements = [‘Wood’, ‘Fire’, ‘Earth’, ‘Metal’, ‘Water’]
 
F getElement(year)
R :elements[(year - 4) % 10 I/ 2]
 
F getAnimal(year)
R :animals[(year - 4) % 12]
 
F getYY(year)
I year % 2 == 0
R ‘yang’
E
R ‘yin’
 
L(year) [1935, 1938, 1968, 1972, 1976, 2017]
print(year‘ is the year of the ’getElement(year)‘ ’getAnimal(year)‘ (’getYY(year)‘).’)</syntaxhighlight>
 
{{out}}
<pre>
1935 is the year of the Wood Pig (yin).
1938 is the year of the Earth Tiger (yang).
1968 is the year of the Earth Monkey (yang).
1972 is the year of the Water Rat (yang).
1976 is the year of the Fire Dragon (yang).
2017 is the year of the Fire Rooster (yin).
</pre>
=={{header|360 Assembly}}==
{{trans|VBScript}}
<syntaxhighlight lang="text">* Chinese zodiac 10/03/2019
CHINEZOD CSECT
USING CHINEZOD,R13 base register
Line 95 ⟶ 126:
XDEC DS CL12 temp for xdeco
REGEQU
END CHINEZOD</langsyntaxhighlight>
{{out}}
<pre>
Line 105 ⟶ 136:
1984 is the year of the Wood Rat (Yang). 1/60
2017 is the year of the Fire Rooster (Yin ). 34/60
</pre>
=={{header|Action!}}==
<syntaxhighlight lang="action!">DEFINE PTR="CARD"
 
PTR ARRAY animals(12),elements(5),stems(10),branches(12),yinYangs(2)
 
PROC Init()
animals(0)="Rat" animals(1)="Ox"
animals(2)="Tiger" animals(3)="Rabbit"
animals(4)="Dragon" animals(5)="Snake"
animals(6)="Horse" animals(7)="Goat"
animals(8)="Monkey" animals(9)="Rooster"
animals(10)="Dog" animals(11)="Pig"
elements(0)="Wood" elements(1)="Fire"
elements(2)="Earth" elements(3)="Metal"
elements(4)="Water"
stems(0)="jia" stems(1)="yi"
stems(2)="bing" stems(3)="ding"
stems(4)="wu" stems(5)="ji"
stems(6)="geng" stems(7)="xin"
stems(8)="ren" stems(9)="gui"
branches(0)="zi" branches(1)="chou"
branches(2)="yin" branches(3)="mao"
branches(4)="chen" branches(5)="si"
branches(6)="wu" branches(7)="wei"
branches(8)="shen" branches(9)="you"
branches(10)="xu" branches(11)="hai"
yinYangs(0)="Yang" yinYangs(1)="Yin"
RETURN
 
PTR FUNC GetAnimal(INT y)
RETURN (animals((y-4) MOD 12))
 
PTR FUNC GetElement(INT y)
RETURN (elements(((y-4) MOD 10)/2))
 
PTR FUNC GetStem(INT y)
RETURN (stems((y-4) MOD 10))
 
PTR FUNC GetBranch(INT y)
RETURN (branches((y-4) MOD 12))
 
PTR FUNC GetYinYang(INT y)
RETURN (yinYangs(y MOD 2))
 
BYTE FUNC GetCycle(INT y)
RETURN ((y-4) MOD 60+1)
 
PROC Main()
INT ARRAY years=[1935 1938 1968 1972 1976 1984 2017 2021]
CHAR ARRAY s
BYTE i,c
INT y
 
Init()
FOR i=0 TO 7
DO
y=years(i)
PrintI(y) Print(" ")
s=GetStem(y) Print(s) Print("-")
s=GetBranch(y) Print(s) Print(" ")
s=GetElement(y) Print(s) Print(" ")
s=GetAnimal(y) Print(s) Print(" ")
s=GetYinYang(y) Print(s) Print(" ")
c=GetCycle(y) PrintB(c) Print("/60") PutE()
OD
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Chinese_zodiac.png Screenshot from Atari 8-bit computer]
<pre>
1935 yi-hai Wood Pig Yin 12/60
1938 wu-yin Earth Tiger Yang 15/60
1968 wu-shen Earth Monkey Yang 45/60
1972 ren-zi Water Rat Yang 49/60
1976 bing-chen Fire Dragon Yang 53/60
1984 jia-zi Wood Rat Yang 1/60
2017 ding-you Fire Rooster Yin 34/60
2021 xin-chou Metal Ox Yin 38/60
</pre>
=={{header|Ada}}==
{{works with|GNAT|8.3.0}}
 
<syntaxhighlight lang="ada">
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
procedure Main is
 
type Element_Index is mod 5;
type Animal_Index is mod 12;
type Stem_Index is mod 10;
type Animal_Array is array(Animal_Index range <>) of Unbounded_String;
type Element_Array is array(Element_Index range <>) of Unbounded_String;
type Stem_Array is array(Stem_Index range <>) of Unbounded_String;
 
Ani_Arr : Animal_Array := (To_Unbounded_String ("Rat"),
To_Unbounded_String ("Ox"),
To_Unbounded_String ("Tiger"),
To_Unbounded_String ("Rabbit"),
To_Unbounded_String ("Dragon"),
To_Unbounded_String ("Snake"),
To_Unbounded_String ("Horse"),
To_Unbounded_String ("Sheep"),
To_Unbounded_String ("Monkey"),
To_Unbounded_String ("Rooster"),
To_Unbounded_String ("Dog"),
To_Unbounded_String ("Pig"));
Bra_Arr : Animal_Array := (To_Unbounded_String ("子"),
To_Unbounded_String ("丑"),
To_Unbounded_String ("寅"),
To_Unbounded_String ("卯"),
To_Unbounded_String ("辰"),
To_Unbounded_String ("巳"),
To_Unbounded_String ("午"),
To_Unbounded_String ("未"),
To_Unbounded_String ("申"),
To_Unbounded_String ("酉"),
To_Unbounded_String ("戌"),
To_Unbounded_String ("亥"));
Ele_Arr : Element_Array := (To_Unbounded_String ("Wood"),
To_Unbounded_String ("Fire"),
To_Unbounded_String ("Earth"),
To_Unbounded_String ("Metal"),
To_Unbounded_String ("Water"));
Ste_Arr : Stem_Array := (To_Unbounded_String ("甲"),
To_Unbounded_String ("乙"),
To_Unbounded_String ("丙"),
To_Unbounded_String ("丁"),
To_Unbounded_String ("戊"),
To_Unbounded_String ("己"),
To_Unbounded_String ("庚"),
To_Unbounded_String ("辛"),
To_Unbounded_String ("壬"),
To_Unbounded_String ("癸"));
procedure Sexagenary (Year : Positive) is
Base_Year : Positive := 1984;
Temp : Natural := abs (Base_Year - Year);
Temp_Float: Float := 0.0;
Ele_Idx : Element_Index := Element_Index'First;
Ani_Idx : Animal_Index := Animal_Index'First;
Ste_Idx : Stem_Index := Stem_Index'First;
Result : Unbounded_String := Null_Unbounded_String;
begin
Result := Result & Year'Image & " is the year of ";
if Year >= Base_Year then
Temp_Float := Float'Floor (Float (Temp) / Float (2));
Ele_Idx := Element_Index (Natural (Temp_Float) mod 5);
Result := Result & Ele_Arr (Ele_Idx) & " ";
Ani_Idx := Animal_Index (Temp mod 12);
Result := Result & Ani_Arr (Ani_Idx) & " ";
elsif Year < Base_Year then
Temp_Float := Float'Ceiling (Float (Temp) / Float (2));
Ele_Idx := Ele_Idx - Element_Index (Natural (Temp_Float) mod 5);
Result := Result & Ele_Arr (Ele_Idx) & " ";
Ani_Idx := Ani_Idx - Animal_Index (Temp mod 12);
Result := Result & Ani_Arr (Ani_Idx) & " ";
end if;
if Year mod 2 = 0 then
Result := Result & "(yang). ";
else
Result := Result & "(yin). ";
end if;
Ani_Idx := Animal_Index'First;
 
if Year >= Base_Year then
Ste_Idx := Stem_Index (Temp mod 10);
Result := Result & Ste_Arr (Ste_Idx);
Ani_Idx := Animal_Index (Temp mod 12);
Result := Result & Bra_Arr (Ani_Idx);
elsif Year < Base_Year then
Ste_Idx := Ste_Idx - Stem_Index (Temp mod 10);
Result := Result & Ste_Arr (Ste_Idx);
Ani_Idx := Ani_Idx - Animal_Index (Temp mod 12);
Result := Result & Bra_Arr (Ani_Idx);
end if;
Put (To_String (Result));
end Sexagenary;
arr : array(Positive range <>) of Positive := (1935, 1938, 1968, 1972, 1976, 1984, 1985, 2017);
begin
for I of arr loop
Sexagenary (I);
New_Line;
end loop;
end Main;
</syntaxhighlight>
 
{{out}}
<pre>
1935 is the year of Wood Pig (yin). 乙亥
1938 is the year of Earth Tiger (yang). 戊寅
1968 is the year of Earth Monkey (yang). 戊申
1972 is the year of Water Rat (yang). 壬子
1976 is the year of Fire Dragon (yang). 丙辰
1984 is the year of Wood Rat (yang). 甲子
1985 is the year of Wood Ox (yin). 乙丑
2017 is the year of Fire Rooster (yin). 丁酉
</pre>
=={{header|ALGOL 68}}==
{{Trans|AWK}}
<syntaxhighlight lang="algol68">BEGIN # Chinese Zodiac #
# returns s right-padded with blanks to w characters, or s if s is already at least w characters long #
PRIO PAD = 1;
OP PAD = ( STRING s, INT w )STRING:
BEGIN
STRING result := s;
WHILE ( ( UPB result + 1 ) - LWB s ) < w DO result +:= " " OD;
result
END # PAD # ;
[]STRING animal name = ( "Rat", "Ox", "Tiger", "Rabbit", "Dragon", "Snake"
, "Horse", "Goat", "Monkey", "Rooster", "Dog", "Pig"
);
[]STRING element name = ( "Wood", "Fire", "Earth", "Metal", "Water" );
[]INT test year = ( 1935, 1938, 1968, 1972, 1976, 1984, 1985, 2017 );
print( ( "year element animal aspect", newline ) );
FOR i FROM LWB test year TO UPB test year DO
INT year = test year[ i ];
STRING element = element name[ ( ( year - 4 ) MOD 10 OVER 2 ) + 1 ];
STRING animal = animal name[ ( year - 4 ) MOD 12 + 1 ];
STRING yy = IF ODD year THEN "Yin" ELSE "Yang" FI;
print( ( whole( year, -4 ), " ", element PAD 7, " ", animal PAD 7, " ", yy, newline ) )
OD
END</syntaxhighlight>
{{out}}
<pre>
year element animal aspect
1935 Wood Pig Yin
1938 Earth Tiger Yang
1968 Earth Monkey Yang
1972 Water Rat Yang
1976 Fire Dragon Yang
1984 Wood Rat Yang
1985 Wood Ox Yin
2017 Fire Rooster Yin
</pre>
=={{header|APL}}==
{{works with|Dyalog APL}}
{{works with|GNU APL}}
 
This simply creates a vector of the attributes of the year.
 
<syntaxhighlight lang="apl">hanzi ← '甲' '乙' '丙' '丁' '戊' '己' '庚' '辛' '壬' '癸' '子' '丑' '寅' '卯' '辰' '巳' '午' '未' '申' '酉' '戌' '亥'
pinyin ← 'jiă' 'yĭ' 'bĭng' 'dīng' 'wù' 'jĭ' 'gēng' 'xīn' 'rén' 'gŭi' 'zĭ' 'chŏu' 'yín' 'măo' 'chén' 'sì' 'wŭ' 'wèi' 'shēn' 'yŏu' 'xū' 'hài'
 
pinyinFor ← { pinyin /⍨ ⍵ ⍷ hanzi }
 
stems ← '甲' '乙' '丙' '丁' '戊' '己' '庚' '辛' '壬' '癸'
branches ← '子' '丑' '寅' '卯' '辰' '巳' '午' '未' '申' '酉' '戌' '亥'
animals ← 'Rat' 'Ox' 'Tiger' 'Rabbit' 'Dragon' 'Snake' 'Horse' 'Goat' 'Monkey' 'Rooster' 'Dog' 'Pig'
elements ← 'Wood' 'Fire' 'Earth' 'Metal' 'Water'
aspects ← 'yang' 'yin'
 
position ← { 1 + 60 | ⍵ - 4 }
item ← { ⍺ ⌷⍨ 1 + (≢⍺) | 1 -⍨ position ⍵ }
celestial ← { stems item ⍵ }
terrestrial ← { branches item ⍵ }
animal ← { animals item ⍵ }
aspect ← { aspects item ⍵ }
element ← { elements ⌷⍨ 1+⌊2÷⍨1×⍨10 | 1 -⍨ position ⍵ }
 
∇vec ← cz year ; cs ; tb
cs ← celestial year
tb ← terrestrial year
vec ← year, (position year), cs, tb, (pinyinFor cs), (pinyinFor tb), (element year), (animal year), (aspect year)
10 1 ⍴ cz ¨ 1935 1938 1941 1944 1947 1968 1972 1976 2003 2006</syntaxhighlight>
 
{{Out}}
<pre>
1935 12 乙亥 yĭ hài Wood Pig yin
1938 15 戊寅 wù yín Earth Tiger yang
1941 18 辛巳 xīn sì Metal Snake yin
1944 21 甲申 jiă shēn Wood Monkey yang
1947 24 丁亥 dīng hài Fire Pig yin
1968 45 戊申 wù shēn Earth Monkey yang
1972 49 壬子 rén zĭ Water Rat yang
1976 53 丙辰 bĭng chén Fire Dragon yang
2003 20 癸未 gŭi wèi Water Goat yin
2006 23 丙戌 bĭng xū Fire Dog yang
</pre>
 
Line 110 ⟶ 434:
{{Trans|JavaScript}}
{{Trans|Haskell}}
<langsyntaxhighlight AppleScriptlang="applescript">on run
-- TRADITIONAL STRINGS ---------------------------------------------------
Line 376 ⟶ 700:
map(result, items 1 thru ¬
minimum({length of xs, length of ys, length of zs}) of xs)
end zip3</langsyntaxhighlight>
{{Out}}
<pre>1935 乙亥 木 豬 阴
Line 405 ⟶ 729:
dīngyŏu huǒ jī yīn
34/60 fire rooster </pre>
 
=={{header|AutoHotkey}}==
Chinese Animal/Element/Yin/Yang Characters Copied From "AppleScript"
<syntaxhighlight lang="autohotkey">Chinese_zodiac(year){
Animal := StrSplit("Rat,Ox,Tiger,Rabbit,Dragon,Snake,Horse,Goat,Monkey,Rooster,Dog,Pig", ",")
AnimalCh := StrSplit("鼠牛虎兔龍蛇馬羊猴鸡狗豬")
AnimalName := StrSplit("shǔ,niú,hǔ,tù,lóng,shé,mǎ,yáng,hóu,jī,gǒu,zhū", ",")
Element := StrSplit("Wood,Fire,Earth,Metal,Water", ",")
ElementCh := StrSplit("木火土金水")
ElementName := StrSplit("mù,huǒ,tǔ,jīn,shuǐ", ",")
StemCh := StrSplit("甲乙丙丁戊己庚辛壬癸")
StemName := StrSplit("jiă,yĭ,bĭng,dīng,wù,jĭ,gēng,xīn,rén,gŭi", ",")
BranchCh := StrSplit("子丑寅卯辰巳午未申酉戌亥")
BranchName := StrSplit("zĭ,chŏu,yín,măo,chén,sì,wŭ,wèi,shēn,yŏu,xū,hài", ",")
Mod10 := Mod(year-4, 10)+1
Mod12 := Mod(year-4, 12)+1
A := Animal[Mod12],
Ac := AnimalCh[Mod12]
An := AnimalName[Mod12]
E := Element[Floor(Mod(year-4, 10)/2+1)]
Ec := ElementCh[Floor(Mod(year-4, 10)/2+1)]
En := ElementName[Floor(Mod(year-4, 10)/2+1)]
YY := Mod(year-4, 2)=1 ? "yīn 阴" : "yáng 阳"
Yr := Mod(year-4, 60)+1 "/60"
S := StemCh[Mod10]
Sn := StemName[Mod10]
B := BranchCh[Mod12]
Bn := BranchName[Mod12]
return year "`t" S B " " Sn "-" Bn " `t" E " " Ec " " En "`t" A " " Ac " " An "`t" YY " " Yr
}</syntaxhighlight>
Examples:<syntaxhighlight lang="autohotkey">loop 12
{
n := 1983+A_Index
output .= Chinese_zodiac(n) "`n"
}
MsgBox % output "`n" Chinese_zodiac(2017)
return</syntaxhighlight>
{{out}}
<pre>1984 甲子 jiă-zĭ Wood 木 mù Rat 鼠 shǔ yáng 阳 1/60
1985 乙丑 yĭ-chŏu Wood 木 mù Ox 牛 niú yīn 阴 2/60
1986 丙寅 bĭng-yín Fire 火 huǒ Tiger 虎 hǔ yáng 阳 3/60
1987 丁卯 dīng-măo Fire 火 huǒ Rabbit 兔 tù yīn 阴 4/60
1988 戊辰 wù-chén Earth 土 tǔ Dragon 龍 lóng yáng 阳 5/60
1989 己巳 jĭ-sì Earth 土 tǔ Snake 蛇 shé yīn 阴 6/60
1990 庚午 gēng-wŭ Metal 金 jīn Horse 馬 mǎ yáng 阳 7/60
1991 辛未 xīn-wèi Metal 金 jīn Goat 羊 yáng yīn 阴 8/60
1992 壬申 rén-shēn Water 水 shuǐ Monkey 猴 hóu yáng 阳 9/60
1993 癸酉 gŭi-yŏu Water 水 shuǐ Rooster 鸡 jī yīn 阴 10/60
1994 甲戌 jiă-xū Wood 木 mù Dog 狗 gǒu yáng 阳 11/60
1995 乙亥 yĭ-hài Wood 木 mù Pig 豬 zhū yīn 阴 12/60
 
2017 丁酉 dīng-yŏu Fire 火 huǒ Rooster 鸡 jī yīn 阴 34/60</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f CHINESE_ZODIAC.AWK
BEGIN {
Line 422 ⟶ 798:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 435 ⟶ 811:
2017 Fire Rooster Yin
</pre>
=={{header|BASIC}}==
 
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">
<lang BASIC256>
# Chinese zodiac
 
Line 462 ⟶ 838:
Next i
End
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 473 ⟶ 849:
2017: 丁酉 (dīng-yŏu, Fire Rooster; Yin - ciclo 34/60)
</pre>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="qbasic">100 rem Chinese zodiac
110 cls
120 dim animals$(12)
130 for i = 0 to ubound(animals$)-1 : read animals$(i) : next i
140 dim elements$(5)
150 for i = 0 to ubound(elements$)-1 : read elements$(i) : next i
160 dim yinyang$(2)
170 yinyang$(0) = "Yang" : yinyang$(1) = "Yin"
180 dim years(7)
190 for i = 0 to ubound(years)-1 : read years(i) : next i
200 for i = 0 to ubound(years)-1
210 xyear = years(i)
220 yelement$ = elements$(((xyear-4) mod 10)/2)
230 yanimal$ = animals$((xyear-4) mod 12)
240 yyinyang$ = yinyang$(xyear mod 2)
250 nn = ((xyear-4) mod 60)+1
260 print xyear "is the year of the " yelement$ " " yanimal$ " (" yyinyang$ ")."
270 next i
280 end
290 data "Rat","Ox","Tiger","Rabbit","Dragon","Snake","Horse","Goat","Monkey","Rooster","Dog","Pig"
300 data "Wood","Fire","Earth","Metal","Water"
310 data 1935,1938,1968,1972,1976,1984,2017</syntaxhighlight>
{{out}}
<pre>Same as Yabasic entry.</pre>
 
==={{header|GW-BASIC}}===
The [[#MSX_BASIC|MSX BASIC]] solution works without any changes.
 
==={{header|MSX Basic}}===
{{works with|Chipmunk Basic}}
{{works with|GW-BASIC}}
{{works with|PC-BASIC}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">100 REM Chinese zodiac
110 CLS
120 PRINT " **** Chinese zodiac ****"
130 DIM animals$(12)
140 FOR i = 0 TO 11
150 READ animals$(i)
160 NEXT i
170 DIM elements$(5)
180 FOR i = 0 TO 4
190 READ elements$(i)
200 NEXT i
210 DIM yinyang$(2)
220 yinyang$(0) = "Yang"
230 yinyang$(1) = "Yin"
240 DIM years(7)
250 FOR i = 0 TO 6
260 READ years(i)
270 NEXT i
280 FOR i = 0 TO 6
290 xyear = years(i)
300 yelement$ = elements$(((xyear-4) MOD 10)/2)
310 yanimal$ = animals$((xyear-4) MOD 12)
320 yyinyang$ = yinyang$(xyear MOD 2)
330 nn = ((xyear-4) MOD 60)+1
340 PRINT xyear; "is the year of the "; yelement$; " "; yanimal$; " ("; yyinyang$; ")."
350 NEXT i
360 END
370 DATA "Rat","Ox","Tiger","Rabbit","Dragon","Snake","Horse","Goat","Monkey","Rooster","Dog","Pig"
380 DATA "Wood","Fire","Earth","Metal","Water"
390 DATA 1935,1938,1968,1972,1976,1984,2017</syntaxhighlight>
{{Out}}
https://www.dropbox.com/s/ckhu3u5pbwq7p5n/Chinese_zodiac%20%28MSX%29.png?dl=0
 
==={{header|IS-BASIC}}===
<syntaxhighlight lang="is-basic">100 PROGRAM "Zodiac.bas"
110 STRING ANIMALS$(0 TO 11)*7,ELEMENTS$(0 TO 4)*5,YINYANG$(0 TO 1)*4
120 FOR I=0 TO 11
130 READ ANIMALS$(I)
140 NEXT
150 FOR I=0 TO 4
160 READ ELEMENTS$(I)
170 NEXT
180 LET YINYANG$(0)="Yang":LET YINYANG$(1)="Yin"
190 CALL ZODIAC(1935):CALL ZODIAC(1938)
200 CALL ZODIAC(1968):CALL ZODIAC(1972)
210 CALL ZODIAC(1976):CALL ZODIAC(2017)
220 DEF ZODIAC(YEAR)
230 LET YELEMENT$=ELEMENTS$(MOD((YEAR-4),10)/2)
240 LET YANIMAL$=ANIMALS$(MOD((YEAR-4),12))
250 LET YYINYANG$=YINYANG$(MOD(YEAR,2))
260 PRINT YEAR;"is the year of the ";YELEMENT$;" ";YANIMAL$;" (";YYINYANG$;")."
270 END DEF
280 DATA Rat,Ox,Tiger,Rabbit,Dragon,Snake, Horse,Goat,Monkey,Rooster,Dog,Pig
290 DATA Wood,Fire,Earth,Metal,Water</syntaxhighlight>
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
{{works with|Chipmunk Basic}}
{{works with|GW-BASIC}}
{{works with|MSX_BASIC}}
{{works with|PC-BASIC}}
<syntaxhighlight lang="qbasic">10 REM Chinese zodiac
20 CLS
30 DIM animals$(12)
40 DIM elements$(5)
50 DIM yinyang$(2)
70 FOR i = 0 TO 11
80 READ animals$(i)
90 NEXT i
110 FOR i = 0 TO 4
120 READ elements$(i)
130 NEXT i
140 yinyang$(0) = "Yang": yinyang$(1) = "Yin"
150 DIM years(7)
170 FOR i = 0 TO 6
180 READ years(i)
190 NEXT i
200 FOR i = 0 TO 6
210 xyear = years(i)
220 yelement$ = elements$(((xyear - 4) MOD 10) / 2)
230 yanimal$ = animals$((xyear - 4) MOD 12)
240 yyinyang$ = yinyang$(xyear MOD 2)
250 nn = ((xyear - 4) MOD 60) + 1
260 PRINT xyear; "is the year of the "; yelement$; " "; yanimal$; " ("; yyinyang$; ")."
270 NEXT i
280 END
290 DATA "Rat","Ox","Tiger","Rabbit","Dragon","Snake","Horse","Goat","Monkey","Rooster","Dog","Pig"
300 DATA "Wood","Fire","Earth","Metal","Water"
310 DATA 1935,1938,1968,1972,1976,1984,2017</syntaxhighlight>
{{out}}
<pre>Same as Yabasic entry.</pre>
 
=={{header|Befunge}}==
 
<langsyntaxhighlight lang="befunge">0" :raeY">:#,_55+"< /8"&>+:66+%00p:55+v
v"Aspect: "0++88*5%2\0\+1%"<":p01++66/2%<
>00g5g7-0" :laminA"10g5g"<"+0" :tnemelE"v
Line 483 ⟶ 987:
$'-4;AGLS[_ %*06yang yin Rat Ox Tiger R |
abbit Dragon Snake Horse Goat Monkey Roo |
ster Dog Pig Wood Fire Earth Metal Water |</langsyntaxhighlight>
 
{{out}}
Line 492 ⟶ 996:
Aspect: yang
Cycle: 35 / 60</pre>
 
=={{header|C}}==
{{trans|C++}}
<langsyntaxhighlight lang="c">#include <math.h>
#include <stdio.h>
 
Line 529 ⟶ 1,032:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>1935 is the year of the Wood Pig (yin).
Line 537 ⟶ 1,040:
1976 is the year of the Fire Dragon (yang).
2017 is the year of the Fire Rooster (yin).</pre>
=={{header|C sharp|C#}}==
{{trans|Java}}
<syntaxhighlight lang="csharp">using System;
 
namespace ChineseZodiac {
class Program {
static string[] animals = { "Rat", "Ox", "Tiger", "Rabbit", "Dragon", "Snake", "Horse", "Goat", "Monkey", "Rooster", "Dog", "Pig" };
static string[] elements = { "Wood", "Fire", "Earth", "Metal", "Water" };
static string[] animalChars = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };
static string[,] elementChars = { { "甲", "丙", "戊", "庚", "壬" }, { "乙", "丁", "己", "辛", "癸" } };
 
static string getYY(int year) {
if (year % 2 == 0) {
return "yang";
}
return "yin";
}
 
static void Main(string[] args) {
Console.OutputEncoding = System.Text.Encoding.UTF8;
int[] years = { 1935, 1938, 1968, 1972, 1976, 1984, 1985, 2017 };
for (int i = 0; i < years.Length; i++) {
int ei = (int)Math.Floor((years[i] - 4.0) % 10 / 2);
int ai = (years[i] - 4) % 12;
Console.WriteLine("{0} is the year of the {1} {2} ({3}). {4}{5}", years[i], elements[ei], animals[ai], getYY(years[i]), elementChars[years[i] % 2, ei], animalChars[(years[i] - 4) % 12]);
}
}
}
}</syntaxhighlight>
{{out}}
<pre>1935 is the year of the Wood Pig (yin). 乙亥
1938 is the year of the Earth Tiger (yang). 戊寅
1968 is the year of the Earth Monkey (yang). 戊申
1972 is the year of the Water Rat (yang). 壬子
1976 is the year of the Fire Dragon (yang). 丙辰
1984 is the year of the Wood Rat (yang). 甲子
1985 is the year of the Wood Ox (yin). 乙丑
2017 is the year of the Fire Rooster (yin). 丁酉</pre>
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <cmath>
 
Line 579 ⟶ 1,119:
}
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 589 ⟶ 1,129:
2017 is the year of the Fire Rooster (yin).
</pre>
 
=={{header|C#|C sharp}}==
{{trans|Java}}
<lang csharp>using System;
 
namespace ChineseZodiac {
class Program {
static string[] animals = { "Rat", "Ox", "Tiger", "Rabbit", "Dragon", "Snake", "Horse", "Goat", "Monkey", "Rooster", "Dog", "Pig" };
static string[] elements = { "Wood", "Fire", "Earth", "Metal", "Water" };
static string[] animalChars = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };
static string[,] elementChars = { { "甲", "丙", "戊", "庚", "壬" }, { "乙", "丁", "己", "辛", "癸" } };
 
static string getYY(int year) {
if (year % 2 == 0) {
return "yang";
}
return "yin";
}
 
static void Main(string[] args) {
Console.OutputEncoding = System.Text.Encoding.UTF8;
int[] years = { 1935, 1938, 1968, 1972, 1976, 1984, 1985, 2017 };
for (int i = 0; i < years.Length; i++) {
int ei = (int)Math.Floor((years[i] - 4.0) % 10 / 2);
int ai = (years[i] - 4) % 12;
Console.WriteLine("{0} is the year of the {1} {2} ({3}). {4}{5}", years[i], elements[ei], animals[ai], getYY(years[i]), elementChars[years[i] % 2, ei], animalChars[(years[i] - 4) % 12]);
}
}
}
}</lang>
{{out}}
<pre>1935 is the year of the Wood Pig (yin). 乙亥
1938 is the year of the Earth Tiger (yang). 戊寅
1968 is the year of the Earth Monkey (yang). 戊申
1972 is the year of the Water Rat (yang). 壬子
1976 is the year of the Fire Dragon (yang). 丙辰
1984 is the year of the Wood Rat (yang). 甲子
1985 is the year of the Wood Ox (yin). 乙丑
2017 is the year of the Fire Rooster (yin). 丁酉</pre>
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(def base-year 4)
(def celestial-stems ["甲" "乙" "丙" "丁" "戊" "己" "庚" "辛" "壬" "癸"])
(def terrestrial-branches ["子" "丑" "寅" "卯" "辰" "巳" "午" "未" "申" "酉" "戌" "亥"])
Line 659 ⟶ 1,159:
(defn -main [& args]
(doseq [years (map read-string args)]
(chinese-zodiac years)))</langsyntaxhighlight>
 
{{out}}
Line 672 ⟶ 1,172:
2017: 丁酉 (dīng-yŏu, Fire Rooster; yin - cycle 34/60)
</pre>
=={{header|Commodore BASIC}}==
{{works with|Commodore 64}}
<syntaxhighlight lang="basic">1000 rem display the chinese zodiac for a given year
1010 poke 53281,7: rem yellow background
1020 poke 53280,2: rem red border
1030 poke 646,2: rem red text
1040 h1$="chinese zodiac":gosub 2000 set-heading
1050 gosub 3000 initialize-data
1060 print
1070 print "enter year (return to quit):";
1080 get k$:if k$="" then 1080
1090 if k$=chr$(13) then end
1100 poke 631,asc(k$):poke 198,1:rem ungetc(k$)
1110 open 1,0: input#1, y$: close 1:print
1120 if val(y$)=0 and y$<>"0" then print chr$(145):goto 1060
1130 y=val(y$)-4
1140 sy=fnym(60): rem year of the sexagesimal cycle
1150 cs=fnym(10): rem celestial stem
1160 tb=fnym(12): rem terrestrial branch
1170 el=int(cs/2): rem element
1180 za=tb: rem zodiac animal
1190 as=fnym(2): rem aspect
1200 print
1210 print "the chinese year beginning in ce "y$
1220 print "is "cs$(cs)"-"tb$(tb)", year"(sy+1)"of 60,"
1230 print "the year of the "el$(el)" "za$(za)" ("as$(as)")."
1260 goto 1060
1270 end
2000 print chr$(147);chr$(18);"****";
2010 sp=32-len(h1$)
2020 for i=1 to int(sp/2)
2030 : print " ";
2040 next i
2050 print h1$;
2060 for i=i to sp
2070 : print " ";
2080 next i
2090 print "****";
2100 return
3000 dim cs$(9): rem ten celestial stems
3010 dim tb$(11): rem twelve terrestrial branches
3020 dim za$(11): rem twelve "zodiac" animals
3030 dim el$(4): rem five elements
3040 dim as$(1): rem two aspects
3050 for i=0 to 9
3060 : read cs$(i)
3070 next i
3080 data jia3, yi3, bing3, ding1, wu4
3090 data ji3, geng1, xin1, ren2, gui3
3100 for i=0 to 11
3110 : read tb$(i)
3120 next i
3130 data zi3, chou3, yin2, mao3
3140 data chen2, si4, wu3, wei4
3150 data shen2, you3, xu1, hai4
3160 for i=0 to 11
3170 : read za$(i)
3180 next i
3190 data rat, ox, tiger, rabbit
3200 data dragon, snake, horse, goat
3210 data monkey, rooster, dog, pig
3220 for i=0 to 4
3230 : read el$(i)
3240 next i
3250 data wood, fire, earth
3260 data metal, water
3270 for i=0 to 1
3280 : read as$(i)
3290 next i
3300 data yang, yin
3310 rem year-mod function
3320 def fnym(d) = y - int(y/d)*d
3330 return</syntaxhighlight>
{{Out}}
https://i.imgur.com/f3Fo1xm.png
 
=={{header|Common Lisp}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="lisp">; Any CE Year that was the first of a 60-year cycle
(defconstant base-year 1984)
 
Line 730 ⟶ 1,305:
(loop for cz in (apply #'chinese-zodiac (mapcar #'read-from-string (get-args)))
doing
(format t "~{~a: ~a~a (~a-~a, ~a ~a; ~a)~%~}" cz))</langsyntaxhighlight>
 
{{Out}}
Line 744 ⟶ 1,319:
1976: 丙辰 (bĭng-chén, Fire Dragon; yang)
</pre>
 
=={{header|D}}==
{{trans|haskell}}
<langsyntaxhighlight Dlang="d">import std.stdio;
 
// 10 heavenly stems
Line 804 ⟶ 1,378:
}
}
</syntaxhighlight>
</lang>
 
{{Out}}
Line 835 ⟶ 1,409:
34/60 fire rooster </pre>
 
=={{header|Dart}}==
<syntaxhighlight lang="dart">Set animals = {'Rat','Ox','Tiger','Rabbit','Dragon','Snake','Horse','Goat','Monkey','Rooster','Dog','Pig'};
Set elements = {'Wood', 'Fire', 'Earth', 'Metal', 'Water'};
 
String getElement(num year) {
num element = ((year - 4) % 10 / 2);
return elements.elementAt(element.floor());
}
 
String getAnimal(int year) {
return animals.elementAt((year - 4) % 12);
}
 
String getYY(int year) {
return (year % 2 == 0) ? 'Yang' : 'Yin';
}
 
void main() {
Set years = {1935, 1938, 1968, 1972, 1976, 2017};
//the zodiac cycle didnt start until 4 CE, so years <4 shouldnt be valid
for (int i = 0; i < 6; i++) {
int indice = years.elementAt(i);
print('$indice is the year of the ${getElement(indice)} ${getAnimal(indice)} (${getYY(indice)}).');
}
}</syntaxhighlight>
{{out}}
<pre>Same as Yabasic entry.</pre>
 
=={{header|Delphi}}==
{{libheader| Winapi.Windows}}
{{libheader| System.SysUtils}}
{{libheader| System.Math}}
{{Trans|C#}}
<syntaxhighlight lang="delphi">
program Chinese_zodiac;
 
{$APPTYPE CONSOLE}
 
uses
Winapi.Windows,
System.SysUtils,
System.Math;
 
type
TElements = array of array of char;
 
TChineseZodiac = record
Year: Integer;
Yyear: string;
Animal: string;
Element: string;
AnimalChar: char;
ElementChar: char;
procedure Assign(aYear: Integer);
function ToString: string;
end;
 
const
animals: TArray<string> = ['Rat', 'Ox', 'Tiger', 'Rabbit', 'Dragon', 'Snake',
'Horse', 'Goat', 'Monkey', 'Rooster', 'Dog', 'Pig'];
elements: TArray<string> = ['Wood', 'Fire', 'Earth', 'Metal', 'Water'];
animalChars: TArray<char> = ['子', '丑', '寅', '卯', '辰', '巳', '午', '未', '申', '酉',
'戌', '亥'];
elementChars: TElements = [['甲', '丙', '戊', '庚', '壬'], ['乙', '丁', '己', '辛', '癸']];
 
 
function getYY(year: Integer): string;
begin
if (year mod 2) = 0 then
begin
Exit('yang');
end;
Exit('yin');
end;
 
{ TChineseZodiac }
 
procedure TChineseZodiac.Assign(aYear: Integer);
var
ei, ai: Integer;
begin
ei := Trunc(Floor((Trunc(aYear - 4.0) mod 10) / 2));
ai := (aYear - 4) mod 12;
year := aYear;
Element := elements[ei];
Animal := animals[ai];
ElementChar := elementChars[aYear mod 2, ei];
AnimalChar := animalChars[(aYear - 4) mod 12];
Yyear := getYY(aYear);
end;
 
var
z: TChineseZodiac;
years: TArray<Integer> = [1935, 1938, 1968, 1972, 1976, 1984, 1985, 2017];
year: integer;
 
function TChineseZodiac.ToString: string;
begin
Result := Format('%d is the year of the %s %s (%s). %s%s', [year, Element,
Animal, Yyear, ElementChar, AnimalChar]);
end;
 
var
Outfile: TextFile;
Written: Cardinal;
 
begin
SetConsoleOutputCP(CP_UTF8);
 
AssignFile(Outfile, 'Output.txt', CP_UTF8);
Rewrite(Outfile);
 
for year in years do
begin
z.Assign(year);
Writeln(Outfile, z.ToString);
Writeln(z.ToString);
end;
 
CloseFile(Outfile);
Readln;
end.</syntaxhighlight>
{{out}}
<pre>
1935 is the year of the Wood Pig (yin). 乙亥
1938 is the year of the Earth Tiger (yang). 戊寅
1968 is the year of the Earth Monkey (yang). 戊申
1972 is the year of the Water Rat (yang). 壬子
1976 is the year of the Fire Dragon (yang). 丙辰
1984 is the year of the Wood Rat (yang). 甲子
1985 is the year of the Wood Ox (yin). 乙丑
2017 is the year of the Fire Rooster (yin). 丁酉</pre>
=={{header|EasyLang}}==
<syntaxhighlight>
animal$[] = [ "Rat" "Ox" "Tiger" "Rabbit" "Dragon" "Snake" "Horse" "Goat" "Monkey" "Rooster" "Dog" "Pig" ]
element$[] = [ "Wood" "Fire" "Earth" "Metal" "Water" ]
yingyang$[] = [ "Yang" "Yin" ]
animal_ch$[] = strchars "子丑寅卯辰巳午未申酉戌亥"
stem_ch$[] = strchars "甲乙丙丁戊己庚辛壬癸"
#
proc get year . el$ an$ yy$ anch$ stch$ .
idx = (year - 4) mod 10
el$ = element$[idx div 2 + 1]
stch$ = stem_ch$[idx + 1]
idx = (year - 4) mod 12
an$ = animal$[idx + 1]
anch$ = animal_ch$[idx + 1]
yy$ = yingyang$[year mod 2 + 1]
.
proc zodiac year . .
get year el$ an$ yy$ anch$ stch$
print year
cycle = (year - 1983) mod1 60
print " " & el$ & " " & an$ & " " & yy$ & " " & cycle & "/60"
print " " & stch$ & anch$
print ""
.
zodiac 1935
zodiac 1938
zodiac 1968
zodiac 1972
zodiac 1976
zodiac 1984
zodiac 2017
</syntaxhighlight>
 
 
=={{header|Excel}}==
===LAMBDA===
 
Binding the name to the following lambda expressions in the Name Manager of the Excel WorkBook:
 
(See [https://www.microsoft.com/en-us/research/blog/lambda-the-ultimatae-excel-worksheet-function/ LAMBDA: The ultimate Excel worksheet function])
 
{{Works with|Office 365 betas 2021}}
<syntaxhighlight lang="lisp">CNZODIAC
=LAMBDA(y,
APPENDCOLS(
APPENDCOLS(
APPENDCOLS(
CNYEARNAME(y)
)(
CNYEARELEMENT(y)
)
)(
CNYEARANIMAL(y)
)
)(
CNYEARYINYANG(y)
)
)
 
 
CNYEARANIMAL
=LAMBDA(y,
LET(
shengxiao, {
"鼠","shǔ","rat";
"牛","niú","ox";
"虎","hǔ","tiger";
"兔","tù","rabbit";
"龍","lóng","dragon";
"蛇","shé","snake";
"馬","mǎ","horse";
"羊","yáng","goat";
"猴","hóu","monkey";
"鸡","jī","rooster";
"狗","gǒu","dog";
"豬","zhū","pig"
},
iYear, y - 4,
iBranch, 1 + MOD(iYear, 12),
TRANSPOSE(
INDEX(shengxiao, iBranch)
)
)
)
 
 
CNYEARELEMENT
=LAMBDA(y,
LET(
wuxing, {
"木","mù","wood";
"火","huǒ","fire";
"土","tǔ","earth";
"金","jīn","metal";
"水","shuǐ","water"
},
iYear, y - 4,
iStem, MOD(iYear, 10),
 
TRANSPOSE(
INDEX(
wuxing,
1 + QUOTIENT(
iStem,
2
)
)
)
)
)
 
 
CNYEARNAME
=LAMBDA(y,
LET(
tiangan, {
"甲","jiă";
"乙","yĭ";
"丙","bĭng";
"丁","dīng";
"戊","wù";
"己","jĭ";
"庚","gēng";
"辛","xīn";
"壬","rén";
"癸","gŭi"
},
dizhi, {
"子","zĭ";
"丑","chŏu";
"寅","yín";
"卯","măo";
"辰","chén";
"巳","sì";
"午","wŭ";
"未","wèi";
"申","shēn";
"酉","yŏu";
"戌","xū";
"亥","hài"
},
 
iYear, y - 4,
iStem, 1 + MOD(iYear, 10),
iBranch, 1 + MOD(iYear, 12),
iIndex, 1 + MOD(iYear, 60),
stem, INDEX(tiangan, iStem),
branch, INDEX(dizhi, iBranch),
APPENDROWS(
APPENDROWS(
CONCAT(INDEX(stem, 1), INDEX(branch, 1))
)(
CONCAT(INDEX(stem, 2), INDEX(branch, 2))
)
)(iIndex & "/60")
)
)
 
 
CNYEARYINYANG
=LAMBDA(y,
LET(
yinyang, {
"阳","yáng", "bright";
"阴","yīn", "dark"
},
TRANSPOSE(
INDEX(
yinyang,
1 + MOD(y - 4, 2)
)
)
)
)</syntaxhighlight>
 
and also assuming the following generic bindings in the Name Manager for the WorkBook:
 
<syntaxhighlight lang="lisp">APPENDCOLS
=LAMBDA(xs,
LAMBDA(ys,
LET(
nx, COLUMNS(xs),
colIndexes, SEQUENCE(1, nx + COLUMNS(ys)),
rowIndexes, SEQUENCE(MAX(ROWS(xs), ROWS(ys))),
 
IFERROR(
IF(nx < colIndexes,
INDEX(ys, rowIndexes, colIndexes - nx),
INDEX(xs, rowIndexes, colIndexes)
),
NA()
)
)
)
)
 
 
APPENDROWS
=LAMBDA(xs,
LAMBDA(ys,
LET(
nx, ROWS(xs),
rowIndexes, SEQUENCE(nx + ROWS(ys)),
colIndexes, SEQUENCE(
1,
MAX(COLUMNS(xs), COLUMNS(ys))
),
 
IFERROR(
IF(rowIndexes <= nx,
INDEX(xs, rowIndexes, colIndexes),
INDEX(ys, rowIndexes - nx, colIndexes)
),
NA()
)
)
)
)</syntaxhighlight>
 
{{Out}}
The formula in cell B2 defines a dynamic array, populating the grid '''B2:E4'''
 
{| class="wikitable"
|-
|||style="text-align:right; font-family:serif; font-style:italic; font-size:120%;"|fx
! colspan="5" style="text-align:left; vertical-align: bottom; font-family:Arial, Helvetica, sans-serif !important;"|=CNZODIAC(A2)
|- style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff;"
|
| A
| B
| C
| D
| E
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 1
| style="font-weight:bold" | Year
| style="font-weight:bold" | Name
| style="font-weight:bold" | Element
| style="font-weight:bold" | Animal
| style="font-weight:bold" | Yinyang
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 2
| style="text-align:right" | 1935
| style="text-align:center; background-color:#cbcefb" | 乙亥
| style="text-align:center" | 木
| style="text-align:center" | 豬
| style="text-align:center" | 阴
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 3
|
| style="text-align:center" | yĭhài
| style="text-align:center" | mù
| style="text-align:center" | zhū
| style="text-align:center" | yīn
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 4
|
| style="text-align:center" | 12/60
| style="text-align:center" | wood
| style="text-align:center" | pig
| style="text-align:center" | dark
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 5
| style="text-align:right" | 1938
| style="text-align:center" | 戊寅
| style="text-align:center" | 土
| style="text-align:center" | 虎
| style="text-align:center" | 阳
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 6
|
| style="text-align:center" | wùyín
| style="text-align:center" | tǔ
| style="text-align:center" | hǔ
| style="text-align:center" | yáng
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 7
|
| style="text-align:center" | 15/60
| style="text-align:center" | earth
| style="text-align:center" | tiger
| style="text-align:center" | bright
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 8
| style="text-align:right" | 1968
| style="text-align:center" | 戊申
| style="text-align:center" | 土
| style="text-align:center" | 猴
| style="text-align:center" | 阳
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 9
|
| style="text-align:center" | wùshēn
| style="text-align:center" | tǔ
| style="text-align:center" | hóu
| style="text-align:center" | yáng
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 10
|
| style="text-align:center" | 45/60
| style="text-align:center" | earth
| style="text-align:center" | monkey
| style="text-align:center" | bright
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 11
| style="text-align:right" | 1972
| style="text-align:center" | 壬子
| style="text-align:center" | 水
| style="text-align:center" | 鼠
| style="text-align:center" | 阳
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 12
|
| style="text-align:center" | rénzĭ
| style="text-align:center" | shuǐ
| style="text-align:center" | shǔ
| style="text-align:center" | yáng
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 13
|
| style="text-align:center" | 49/60
| style="text-align:center" | water
| style="text-align:center" | rat
| style="text-align:center" | bright
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 14
| style="text-align:right" | 1976
| style="text-align:center" | 丙辰
| style="text-align:center" | 火
| style="text-align:center" | 龍
| style="text-align:center" | 阳
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 15
|
| style="text-align:center" | bĭngchén
| style="text-align:center" | huǒ
| style="text-align:center" | lóng
| style="text-align:center" | yáng
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 16
|
| style="text-align:center" | 53/60
| style="text-align:center" | fire
| style="text-align:center" | dragon
| style="text-align:center" | bright
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 17
| style="text-align:right" | 1984
| style="text-align:center" | 甲子
| style="text-align:center" | 木
| style="text-align:center" | 鼠
| style="text-align:center" | 阳
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 18
|
| style="text-align:center" | jiăzĭ
| style="text-align:center" | mù
| style="text-align:center" | shǔ
| style="text-align:center" | yáng
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 19
|
| style="text-align:center" | 1/60
| style="text-align:center" | wood
| style="text-align:center" | rat
| style="text-align:center" | bright
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 20
| style="text-align:right" | 2017
| style="text-align:center" | 丁酉
| style="text-align:center" | 火
| style="text-align:center" | 鸡
| style="text-align:center" | 阴
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 21
|
| style="text-align:center" | dīngyŏu
| style="text-align:center" | huǒ
| style="text-align:center" | jī
| style="text-align:center" | yīn
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 22
|
| style="text-align:center" | 34/60
| style="text-align:center" | fire
| style="text-align:center" | rooster
| style="text-align:center" | dark
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 23
| style="text-align:right" | 2021
| style="text-align:center" | 辛丑
| style="text-align:center" | 金
| style="text-align:center" | 牛
| style="text-align:center" | 阴
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 24
|
| style="text-align:center" | xīnchŏu
| style="text-align:center" | jīn
| style="text-align:center" | niú
| style="text-align:center" | yīn
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 25
|
| style="text-align:center" | 38/60
| style="text-align:center" | metal
| style="text-align:center" | ox
| style="text-align:center" | dark
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 26
| style="text-align:right" | 2022
| style="text-align:center" | 壬寅
| style="text-align:center" | 水
| style="text-align:center" | 虎
| style="text-align:center" | 阳
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 27
|
| style="text-align:center" | rényín
| style="text-align:center" | shuǐ
| style="text-align:center" | hǔ
| style="text-align:center" | yáng
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 28
|
| style="text-align:center" | 39/60
| style="text-align:center" | water
| style="text-align:center" | tiger
| style="text-align:center" | bright
|}
 
=={{header|F Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
open System
 
Line 859 ⟶ 2,004:
Console.ReadLine() |> ignore
0 // return an integer exit code
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 871 ⟶ 2,016:
2017 is the year of the Fire Rooster (Yin)
</pre>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: circular formatting io kernel math qw sequences
sequences.repeating ;
IN: rosetta-code.zodiac
Line 903 ⟶ 2,047:
[ zodiac print ] each ;
 
MAIN: zodiac-demo</langsyntaxhighlight>
{{out}}
<pre>
Line 915 ⟶ 2,059:
2017 is the year of the Fire Rooster (yin).
</pre>
=={{header|Forth}}==
{{works with|gforth|0.7.3}}
 
<syntaxhighlight lang="forth">
: position ( n -- n ) 4 - 60 mod ;
 
s" Rat Ox Tiger Rabbit Dragon Snake Horse Goat Monkey RoosterDog Pig "
drop constant animals
: animal ( n -- a u ) 12 mod 7 * animals + 7 -trailing ;
 
s" Wood Fire EarthMetalWater" drop constant elements
: element ( n -- a u ) 2/ 5 mod 5 * elements + 5 -trailing ;
 
s" yangyin " drop constant aspects
: aspect ( n -- a u ) 2 mod 4 * aspects + 4 -trailing ;
 
next-arg s>number? drop d>s position
dup element type space
dup animal type space
aspect '(' emit type ')' emit cr
bye
</syntaxhighlight>
 
{{out}}
<pre>
; for (x in 1935 1938 1968 1972 1976 1984 2017) {printf '%s ' $x; gforth zodiac.f $x}
1935 Wood Pig (yin)
1938 Earth Tiger (yang)
1968 Earth Monkey (yang)
1972 Water Rat (yang)
1976 Fire Dragon (yang)
1984 Wood Rat (yang)
2017 Fire Rooster (yin)
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">dim as string yy(0 to 1) = {"yang", "yin"}
dim as string elements(0 to 4) = {"Wood", "Fire", "Earth", "Metal", "Water"}
dim as string animals(0 to 11) = {"Rat", "Ox", "Tiger", "Rabbit", "Dragon", "Snake",_
"Horse", "Goat", "Monkey", "Rooster", "Dog", "Pig"}
 
dim as uinteger yr, y, e, a, i, tests(0 to 5) = {1801, 1861, 1984, 2020, 2186, 76543}
dim as string outstr
 
for i = 0 to 5
yr = tests(i)
y = yr mod 2
e = (yr - 4) mod 5
a = (yr - 4) mod 12
outstr = str(yr)+" is the year of the "
outstr += elements(e)+" " + animals(a) + " (" + yy(y) + ")."
print outstr
next i</syntaxhighlight>
{{out}}
<pre>
1801 is the year of the Earth Rooster (yin).
1861 is the year of the Earth Rooster (yin).
1984 is the year of the Wood Rat (yang).
2020 is the year of the Fire Rat (yang).
2186 is the year of the Earth Dog (yang).
76543 is the year of the Water Rabbit (yin).</pre>
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Chinese_zodiac}}
 
'''Solution'''
 
[[File:Fōrmulæ - Chinese zodiac 01.png]]
 
'''Test case'''
 
[[File:Fōrmulæ - Chinese zodiac 02.png]]
 
[[File:Fōrmulæ - Chinese zodiac 03.png]]
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 947 ⟶ 2,166:
yr, e, a, yy, cy, sb)
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 956 ⟶ 2,175:
1976: Fire Dragon, Yang, Cycle year 53 丙辰
</pre>
=={{header|Groovy}}==
{{trans|Java}}
<syntaxhighlight lang="groovy">class Zodiac {
final static String[] animals = ["Rat", "Ox", "Tiger", "Rabbit", "Dragon", "Snake", "Horse", "Goat", "Monkey", "Rooster", "Dog", "Pig"]
final static String[] elements = ["Wood", "Fire", "Earth", "Metal", "Water"]
final static String[] animalChars = ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"]
static String[][] elementChars = [["甲", "丙", "戊", "庚", "壬"], ["乙", "丁", "己", "辛", "癸"]]
 
static String getYY(int year) {
if (year % 2 == 0) {
return "yang"
} else {
return "yin"
}
}
 
static void main(String[] args) {
int[] years = [1935, 1938, 1968, 1972, 1976, 1984, 1985, 2017]
for (int i = 0; i < years.length; i++) {
println(years[i] + " is the year of the " + elements[(int) Math.floor((years[i] - 4) % 10 / 2)] + " " + animals[(years[i] - 4) % 12] + " (" + getYY(years[i]) + "). " + elementChars[years[i] % 2][(int) Math.floor((years[i] - 4) % 10 / 2)] + animalChars[(years[i] - 4) % 12])
}
}
}</syntaxhighlight>
{{out}}
<pre>1935 is the year of the Wood Pig (yin). 乙亥
1938 is the year of the Earth Tiger (yang). 戊寅
1968 is the year of the Earth Monkey (yang). 戊申
1972 is the year of the Water Rat (yang). 壬子
1976 is the year of the Fire Dragon (yang). 丙辰
1984 is the year of the Wood Rat (yang). 甲子
1985 is the year of the Wood Ox (yin). 乙丑
2017 is the year of the Fire Rooster (yin). 丁酉</pre>
=={{header|Haskell}}==
(We can use Chinese characters in Haskell names, as long as the first character is lower-case alphabetic)
<langsyntaxhighlight lang="haskell">import Data.Array (Array, listArray, (!))
 
-- TRADITIONAL STRINGS ------------------------------------ TRADITIONAL STRINGS ------------------
ats :: Array Int (Char, String)
ats =
listArray (0, 9) $
zip
"甲乙丙丁戊己庚辛壬癸" -- 天干 tiangan – 10 heavenly stems
"甲乙丙丁戊己庚辛壬癸"
(words "jiă yĭ bĭng dīng wù jĭ gēng xīn rén gŭi")
(words "jiă yĭ bĭng dīng wù jĭ gēng xīn rén gŭi")
 
ads :: Array Int (String, String)
ads =
listArray (0, 11) $
zip
(chars "子丑寅卯辰巳午未申酉戌亥") -- 地支 dizhi – 12 terrestrial branches
(chars "子丑寅卯辰巳午未申酉戌亥")
(words "zĭ chŏu yín măo chén sì wŭ wèi shēn yŏu xū hài")
( words $
"zĭ chŏu yín măo chén sì "
<> "wŭ wèi shēn yŏu xū hài"
)
 
aws :: Array Int (String, String, String)
aws =
listArray (0, 4) $
zip3
(chars "木火土金水") -- 五行 wuxing – 5 elements
(wordschars "mù huǒ tǔ jīn shuǐ木火土金水")
(words "wood firehuǒ earth metaljīn watershuǐ")
(words "wood fire earth metal water")
 
axs :: Array Int (String, String, String)
axs =
listArray (0, 11) $
zip3
(chars "鼠牛虎兔龍蛇馬羊猴鸡狗豬") -- 十二生肖 shengxiao – 12 symbolic animals
(chars "鼠牛虎兔龍蛇馬羊猴鸡狗豬")
(words "shǔ niú hǔ tù lóng shé mǎ yáng hóu jī gǒu zhū")
( words $
(words "rat ox tiger rabbit dragon snake horse goat monkey rooster dog pig")
"shǔ niú hǔ tù lóng shé "
<> "mǎ yáng hóu jī gǒu zhū"
)
( words $
"rat ox tiger rabbit dragon snake "
<> "horse goat monkey rooster dog pig"
)
 
ays :: Array Int (String, String)
ays = listArray (0, 1) $ zip (chars "阳阴") (words "yáng yīn") -- 阴阳 yinyang
ays =
listArray (0, 1) $
zip (chars "阳阴") (words "yáng yīn")
 
chars :: String -> [String]
chars = (flip (:) [] <$>)
 
-- TRADITIONAL CYCLES -------------------------------------- TRADITIONAL CYCLES ------------------
f生肖五行年份 y =
let i年份 = y - 4
Line 1,008 ⟶ 2,274:
(h生肖, p生肖, e生肖) = axs ! i地支
(h阴阳, p阴阳) = ays ! rem i年份 2
in [ [show y, h天干 : h地支, h五行, h生肖, h阴阳] -- 汉子 Chinese characters
, [ []show y, ph天干 ++: ph地支, ph五行, ph生肖, ph阴阳] -- Pinyin roman transcription,
-- Pinyin roman transcription
, [[], show (rem i年份 60 + 1) ++ "/60", e五行, e生肖, []] -- English
[[], p天干 <> p地支, p五行, p生肖, p阴阳],
]
-- English
[ [],
show (rem i年份 60 + 1) <> "/60",
e五行,
e生肖,
[]
]
]
 
--------------------------- TEST -------------------------
main :: IO ()
main =
mapM_ putStrLn $
showYear
<$> [1935, 1938, 1968, 1972, 1976, 1984, 2017]
 
-- FORMATTING ------------------------------------------ FORMATTING ----------------------
fieldWidths :: [[Int]]
fieldWidths =
fieldWidths = [[6, 10, 7, 8, 3], [6, 11, 8, 8, 4], [6, 11, 8, 8, 4]]
[ [6, 10, 7, 8, 3],
[6, 11, 8, 8, 4],
[6, 11, 8, 8, 4]
]
 
showYear :: Int -> String
showYear y =
unlines $
( \(ns, xs) -> concat $ uncurry (`justifyLeft` ' ') <$> zip ns xs) <$>
concat $
zip fieldWidths (f生肖五行年份 y)
uncurry (`justifyLeft` ' ')
<$> zip ns xs
)
<$> zip fieldWidths (f生肖五行年份 y)
where
justifyLeft n c s = take n (s ++<> replicate n c)</syntaxhighlight>
 
-- TEST OUTPUT ---------------------------------------------------------------
main :: IO ()
main = mapM_ putStrLn $ showYear <$> [1935, 1938, 1968, 1972, 1976, 1984, 2017]</lang>
{{Out}}
<pre>1935 乙亥 木 豬 阴
Line 1,056 ⟶ 2,341:
dīngyŏu huǒ jī yīn
34/60 fire rooster </pre>
 
=={{header|J}}==
<langsyntaxhighlight Jlang="j"> ELEMENTS=: _4 |. 2 # ;:'Wood Fire Earth Metal Water'
YEARS=: 1935 1938 1968 1972 1976 2017
 
Line 1,099 ⟶ 2,383:
1976 丙 辰 ( yang )
2017 丁 酉 ( yin )
</syntaxhighlight>
</lang>
 
=={{header|Java}}==
<langsyntaxhighlight Javalang="java">public class Zodiac {
 
final static String animals[]={"Rat","Ox","Tiger","Rabbit","Dragon","Snake","Horse","Goat","Monkey","Rooster","Dog","Pig"};
Line 1,130 ⟶ 2,413:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,142 ⟶ 2,425:
2017 is the year of the Fire Rooster (yin). 丁酉
</pre>
 
=={{header|JavaScript}}==
 
===ES6===
{{Trans|Haskell}}
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
'"use strict'";
 
// GENERIC FUNCTIONS ------------------------------------------ TRADITIONAL CALENDAR STRINGS -----------
 
// charsats :: StringArray ->Int [Char](String, String)
const charsats = s() => s.split('');
// 天干 tiangan – 10 heavenly stems
 
zip(
// concat :: [[a]] -> [a] | [String] -> String
chars("甲乙丙丁戊己庚辛壬癸")
const concat = xs =>
xs.length > 0 ? )(() => {
constwords("jiă unit =bĭng typeofdīng xs[0] === 'string'gēng ?xīn ''rén : [];gŭi")
return unit.concat.apply(unit, xs);
})() : [];
 
// intercalate :: String -> [a] -> String
const intercalate = (s, xs) => xs.join(s);
 
// justifyLeft :: Int -> Char -> Text -> Text
const justifyLeft = (n, cFiller, strText) =>
n > strText.length ? (
(strText + cFiller.repeat(n))
.substr(0, n)
) : strText;
 
// map :: (a -> b) -> [a] -> [b]
const map = (f, xs) => xs.map(f);
 
// quot :: Int -> Int -> Int
const quot = (n, m) => Math.floor(n / m);
 
// show :: Int -> a -> Indented String
// show :: a -> String
const show = (...x) =>
JSON.stringify.apply(
null, x.length > 1 ? [x[1], null, x[0]] : x
);
 
// unlines :: [String] -> String
const unlines = xs => xs.join('\n');
 
// words :: String -> [String]
const words = s => s.split(/\s+/);
 
// zip :: [a] -> [b] -> [(a,b)]
const zip = (xs, ys) =>
xs.slice(0, Math.min(xs.length, ys.length))
.map((x, i) => [x, ys[i]]);
 
// zip3 :: [a] -> [b] -> [c] -> [(a,b,c)]
const zip3 = (xs, ys, zs) =>
xs.slice(0, Math.min(xs.length, ys.length, zs.length))
.map((x, i) => [x, ys[i], zs[i]]);
 
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
const zipWith = (f, xs, ys) =>
Array.from({
length: Math.min(xs.length, ys.length)
}, (_, i) => f(xs[i], ys[i]));
 
// TRADITIONAL STRINGS ---------------------------------------------------
// ats :: Array Int (String, String)
const ats = zip(
chars("甲乙丙丁戊己庚辛壬癸"), // 天干 tiangan – 10 heavenly stems
words("jiă yĭ bĭng dīng wù jĭ gēng xīn rén gŭi")
);
 
// ads :: Array Int (String, String)
const ads = zip() =>
chars("子丑寅卯辰巳午未申酉戌亥"), // 地支 dizhi – 12 terrestrial branches
zip(
words("zĭ chŏu yín măo chén sì wŭ wèi shēn yŏu xū hài")
chars("子丑寅卯辰巳午未申酉戌亥")
);
)(
words(
"zĭ chŏu yín măo chén sì " + (
"wŭ wèi shēn yŏu xū hài"
)
)
);
 
 
// aws :: Array Int (String, String, String)
const aws = zip3() =>
chars("木火土金水"), // 五行 wuxing – 5 elements
wordszip3("mù huǒ tǔ jīn shuǐ"),
words("wood fire earth metal waterchars("木火土金水")
);(
words("mù huǒ tǔ jīn shuǐ")
)(
words("wood fire earth metal water")
);
 
 
// axs :: Array Int (String, String, String)
const axs = zip3() =>
chars("鼠牛虎兔龍蛇馬羊猴鸡狗豬"), // 十二生肖 shengxiao – 12 symbolic animals
zip3(
words("shǔ niú hǔ tù lóng shé mǎ yáng hóu jī gǒu zhū"),
chars("鼠牛虎兔龍蛇馬羊猴鸡狗豬")
words("rat ox tiger rabbit dragon snake horse goat monkey rooster dog pig")
);(
words(
"shǔ niú hǔ tù lóng shé " + (
"mǎ yáng hóu jī gǒu zhū"
)
)
)(
words(
"rat ox tiger rabbit dragon snake " + (
"horse goat monkey rooster dog pig"
)
)
);
 
 
// ays :: Array Int (String, String)
const ays = zip() =>
chars("阳阴"), // 阴阳 yinyang
wordszip("yáng yīn")
chars("阳阴")
);
)(
words("yáng yīn")
);
 
 
// TRADITIONAL CYCLES ----------------------------------------------------
// --------------- TRADITIONAL CYCLES ----------------
const zodiac = y => {
const
Line 1,246 ⟶ 2,506:
iStem = iYear % 10,
iBranch = iYear % 12,
[hStem, pStem] = ats()[iStem],
[hBranch, pBranch] = ads()[iBranch],
[hElem, pElem, eElem] = aws()[quot(iStem, )(2)],
[hAnimal, pAnimal, eAnimal] = axs()[iBranch],
[hYinyang, pYinyang] = ays()[iYear % 2];
 
return [
[
[show(y), hStem + hBranch, hElem, hAnimal, hYinyang],
['', pStem + pBranch, pElemshow(y), pAnimalhStem + hBranch, pYinyang]hElem,
['', show((iYear % 60) + 1) + '/60', eElem, eAnimalhAnimal, '']hYinyang
],
["", pStem + pBranch, pElem, pAnimal, pYinyang],
[
"", `${show((iYear % 60) + 1)}/60`,
eElem, eAnimal, ""
]
];
};
 
 
// FORMATTING ------------------------------------------------------------
// ---------------------- TEST -----------------------
const main = () => [
1935, 1938, 1968, 1972, 1976, 1984,
new Date().getFullYear()
]
.map(showYear)
.join("\n\n");
 
 
// ------------------- FORMATTING --------------------
// fieldWidths :: [[Int]]
const fieldWidths = [
Line 1,265 ⟶ 2,542:
[6, 11, 8, 8, 4]
];
 
 
// showYear :: Int -> String
const showYear = y =>
unlineszipWith(mapzip)(fieldWidths)(zodiac(y))
.map(
row => concat(map(([n, s]) => justifyLeft(n, ' ', s), row)),
zipWith(zip,row fieldWidths,=> zodiacrow.map(y))
([n, s]) => s.padEnd(n, " ");
)
.join("")
)
.join("\n");
 
 
// TEST OUTPUT -----------------------------------------------------------
// ---------------- GENERIC FUNCTIONS ----------------
return intercalate(
 
'\n\n',
// chars :: String map(-> [Char]
const chars = s => [...s];
showYear, [1935, 1938, 1968, 1972, 1976, 1984, new Date()
 
.getFullYear()
 
]
// quot :: Integral a => a -> a -> a
const quot = n =>
m => Math.trunc(n / m);
 
 
// show :: Int -> a -> Indented String
// show :: a -> String
const show = (...x) =>
JSON.stringify.apply(
null, x.length > 1 ? [
x[1], null, x[0]
] : x
);
 
 
// words :: String -> [String]
const words = s =>
// List of space-delimited sub-strings.
s.split(/\s+/u);
 
 
// zip :: [a] -> [b] -> [(a, b)]
const zip = xs =>
// The paired members of xs and ys, up to
// the length of the shorter of the two lists.
ys => Array.from({
length: Math.min(xs.length, ys.length)
}, (_, i) => [xs[i], ys[i]]);
 
 
// zip3 :: [a] -> [b] -> [c] -> [(a, b, c)]
const zip3 = xs =>
ys => zs => xs.slice(
0,
Math.min(...[xs, ys, zs].map(x => x.length))
)
.map((x, i) => [x, ys[i], zs[i]]);
);
 
})();</lang>
 
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
const zipWith = f =>
// A list constructed by zipping with a
// custom function, rather than with the
// default tuple constructor.
xs => ys => xs.map(
(x, i) => f(x)(ys[i])
).slice(
0, Math.min(xs.length, ys.length)
);
 
 
// MAIN ---
return main();
})();</syntaxhighlight>
{{Out}}
<pre>1935 乙亥 木 豬 阴
Line 1,308 ⟶ 2,641:
1/60 wood rat
 
20172022 丁酉壬寅
dīngyŏurényín huǒ shuǐ yīn yáng
3439/60 firewater rooster tiger </pre>
=={{header|jq}}==
'''Works with jq and gojq, that is, the C and Go implementations of jq.'''
 
'''Adapted from [[#Wren|Wren]]'''
<syntaxhighlight lang=jq>
def ChineseZodiac: {
animals : ["Rat", "Ox", "Tiger", "Rabbit", "Dragon", "Snake",
"Horse", "Goat", "Monkey", "Rooster", "Dog", "Pig"],
aspects : ["Yang","Yin"],
elements : ["Wood", "Fire", "Earth", "Metal", "Water"],
stems : ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"],
branches : ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"],
sNames : ["jiă", "yĭ", "bĭng", "dīng", "wù", "jĭ", "gēng", "xīn", "rén", "gŭi"],
bNames : ["zĭ", "chŏu", "yín", "măo", "chén", "sì", "wŭ", "wèi", "shēn", "yŏu", "xū", "hài"]
};
 
def ChineseZodiac($year):
($year - 4) as $y
| ($y % 10) as $s
| ($y % 12) as $b
| ChineseZodiac
| { year : $year,
stem : .stems[$s],
branch : .branches[$b],
sName : .sNames[$s],
bName : .bNames[$b],
element : .elements[($s/2)|floor],
animal : .animals[$b],
aspect : .aspects[$s % 2],
cycle : ($y % 60 + 1)
};
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
# Input: ChineseZodiac($year)
def toString:
(.sName + "-" + .bName | lpad(9)) as $name
| (.element|lpad(7)) as $elem
| (.animal|lpad(7)) as $anim
| (.aspect|lpad(6)) as $aspt
| ((.cycle|lpad(2)) + "/60") as $cycl
| "\(.year) \(.stem)\(.branch) \($name) \($elem) \($anim) \($aspt) \($cycl)" ;
 
 
"Year Chinese Pinyin Element Animal Aspect Cycle",
"---- ------- --------- ------- ------- ------ -----",
(ChineseZodiac(1935, 1938, 1968, 1972, 1976, 1984, 2017, 2020) | toString)
</syntaxhighlight>
{{output}}
<pre>
Year Chinese Pinyin Element Animal Aspect Cycle
---- ------- --------- ------- ------- ------ -----
1935 乙亥 yĭ-hài Wood Pig Yin 12/60
1938 戊寅 wù-yín Earth Tiger Yang 15/60
1968 戊申 wù-shēn Earth Monkey Yang 45/60
1972 壬子 rén-zĭ Water Rat Yang 49/60
1976 丙辰 bĭng-chén Fire Dragon Yang 53/60
1984 甲子 jiă-zĭ Wood Rat Yang 1/60
2017 丁酉 dīng-yŏu Fire Rooster Yin 34/60
2020 庚子 gēng-zĭ Metal Rat Yang 37/60
</pre>
 
=={{header|Julia}}==
Line 1,316 ⟶ 2,710:
{{trans|Python}}
 
<langsyntaxhighlight lang="julia">function chinese(year::Int)
pinyin = Dict(
"甲" => "jiă",
Line 1,373 ⟶ 2,767:
curryr = Dates.year(now())
yrs = [1935, 1938, 1968, 1972, 1976, curryr]
foreach(println, map(chinese, yrs))</langsyntaxhighlight>
 
{{out}}
Line 1,384 ⟶ 2,778:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.2
 
class ChineseZodiac(val year: Int) {
Line 1,431 ⟶ 2,825:
println("---- ------- --------- ------- ------- ------ -----")
for (year in years) println(ChineseZodiac(year))
}</langsyntaxhighlight>
 
{{out}}
Line 1,445 ⟶ 2,839:
2017 丁酉 dīng-yŏu Fire Rooster Yin 34/60
</pre>
 
=={{header|Lua}}==
{{trans|C}}
<langsyntaxhighlight lang="lua">local ANIMALS = {"Rat","Ox","Tiger","Rabbit","Dragon","Snake","Horse","Goat","Monkey","Rooster","Dog","Pig"}
local ELEMENTS = {"Wood","Fire","Earth","Metal","Water"}
 
Line 1,481 ⟶ 2,874:
zodiac(1972)
zodiac(1976)
zodiac(2017)</langsyntaxhighlight>
{{out}}
<pre>1935 is the year of the Wood Pig (yin)
Line 1,489 ⟶ 2,882:
1976 is the year of the Fire Dragon (yang)
2017 is the year of the Fire Rooster (yin)</pre>
 
=={{header|Maple}}==
<syntaxhighlight lang="maple">
<lang Maple>
zodiac:=proc(year::integer)
local year60,yinyang,animal,element;
Line 1,500 ⟶ 2,892:
return sprintf("%a",cat(year," is the year of the ",element[(year60 mod 10)+1]," ",animal[(year60 mod 12)+1]," (",yinyang[(year60 mod 2)+1],")"));
end proc:
</syntaxhighlight>
</lang>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
 
<syntaxhighlight lang="mathematica">pinyin = <|"甲" -> "jiă", "乙" -> "yĭ", "丙" -> "bĭng", "丁" -> "dīng",
"戊" -> "wù", "己" -> "jĭ", "庚" -> "gēng", "辛" -> "xīn",
"壬" -> "rén", "癸" -> "gŭi", "子" -> "zĭ", "丑" -> "chŏu",
"寅" -> "yín", "卯" -> "măo", "辰" -> "chén", "巳" -> "sì",
"午" -> "wŭ", "未" -> "wèi", "申" -> "shēn", "酉" -> "yŏu",
"戌" -> "xū", "亥" -> "hài"|>;
animals = {"Rat", "Ox", "Tiger", "Rabbit", "Dragon", "Snake", "Horse",
"Goat", "Monkey", "Rooster", "Dog", "Pig"};
elements = {"Wood", "Fire", "Earth", "Metal", "Water"};
celestial = {"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"};
terrestrial = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌",
"亥"};
aspects = {"yang", "yin"};
ClearAll[calculate]
calculate[year_] := Module[{BASE, cycleyear, stemnumber, stempinyin, element,
elementnumber, branchnumber, branchhan, branchpinyin, animal,
aspectnumber, aspect, stemhan},
BASE = 4;
cycleyear = year - BASE;
stemnumber = Mod[cycleyear + 1, 10, 1];
stemhan = celestial[[stemnumber]];
stempinyin = pinyin[[stemhan]];
elementnumber = Floor[stemnumber/2 + 1/2];
element = elements[[elementnumber]];
branchnumber = Mod[cycleyear + 1, 12, 1];
branchhan = terrestrial[[branchnumber]];
branchpinyin = pinyin[[branchhan]];
animal = animals[[branchnumber]];
aspectnumber = Mod[cycleyear + 1, 2, 1];
aspect = aspects[[aspectnumber]];
Row@{year, ": ", stemhan, branchhan, " (", stempinyin, "-", branchpinyin, ", ", element, " ", animal, "; ", aspect, ")"}
]
calculate[1935]
calculate[1938]
calculate[1941]
calculate[1947]
calculate[1968]
calculate[1972]
calculate[1976]</syntaxhighlight>
{{out}}
<pre>1935: 乙亥 (yĭ-hài, Wood Pig; yin)
1938: 戊寅 (wù-yín, Earth Tiger; yang)
1941: 辛巳 (xīn-sì, Metal Snake; yin)
1947: 丁亥 (dīng-hài, Fire Pig; yin)
1968: 戊申 (wù-shēn, Earth Monkey; yang)
1972: 壬子 (rén-zĭ, Water Rat; yang)
1976: 丙辰 (bĭng-chén, Fire Dragon; yang)</pre>
=={{header|Modula-2}}==
{{trans|C++}}
<langsyntaxhighlight lang="modula2">MODULE ChineseZodiac;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,ReadChar;
Line 1,556 ⟶ 2,995:
 
ReadChar
END ChineseZodiac.</langsyntaxhighlight>
=={{header|Nim}}==
{{trans|Rust}}
<syntaxhighlight lang="nim">import strformat
 
const ANIMALS: array[12, string] = ["Rat", "Ox",
"Tiger", "Rabbit",
"Dragon", "Snake",
"Horse", "Goat",
"Monkey", "Rooster",
"Dog", "Pig"]
 
const ASPECTS: array[2, string] = ["Yang", "Yin"]
 
const ELEMENTS: array[5, string] = ["Wood", "Fire",
"Earth", "Metal",
"Water"]
 
const STEMS: array[10, string] = ["甲", "乙", "丙", "丁", "戊",
"己", "庚", "辛", "壬", "癸"]
 
const BRANCHES: array[12, string] = ["子", "丑", "寅", "卯", "辰", "巳",
"午", "未", "申", "酉", "戌", "亥"]
 
const S_NAMES: array[10, string] = ["jiă", "yĭ", "bĭng", "dīng", "wù",
"jĭ", "gēng", "xīn", "rén", "gŭi"]
 
const B_NAMES: array[12, string] = ["zĭ", "chŏu", "yín", "măo", "chén", "sì",
"wŭ", "wèi", "shēn", "yŏu", "xū", "hài"]
 
proc chineseZodiac(year: int): string =
let y = year - 4
let s = y mod 10
let b = y mod 12
let stem = STEMS[s]
let branch = BRANCHES[b]
let name = S_NAMES[s] & "-" & B_NAMES[b]
let element = ELEMENTS[s div 2]
let animal = ANIMALS[b]
let aspect = ASPECTS[s mod 2]
let cycle = y mod 60 + 1
&"{year} {stem}{branch} {name:9} {element:7} {animal:7} {aspect:6} {cycle:02}/60"
 
let years = [1935, 1938, 1968, 1972, 1976, 1984, 2017, 2020]
echo "Year Chinese Pinyin Element Animal Aspect Cycle"
echo "---- ------- ------ ------- ------ ------ -----"
for year in years:
echo &"{chineseZodiac(year)}"</syntaxhighlight>
 
{{out}}
<pre>
Year Chinese Pinyin Element Animal Aspect Cycle
---- ------- ------ ------- ------ ------ -----
1935 乙亥 yĭ-hài Wood Pig Yin 12/60
1938 戊寅 wù-yín Earth Tiger Yang 15/60
1968 戊申 wù-shēn Earth Monkey Yang 45/60
1972 壬子 rén-zĭ Water Rat Yang 49/60
1976 丙辰 bĭng-chén Fire Dragon Yang 53/60
1984 甲子 jiă-zĭ Wood Rat Yang 01/60
2017 丁酉 dīng-yŏu Fire Rooster Yin 34/60
2020 庚子 gēng-zĭ Metal Rat Yang 37/60
</pre>
=={{header|Pascal}}==
{{works with|Extended Pascal}}
The built-in functions <tt>pred</tt> and <tt>succ</tt> accepting a second (optional) parameter is an Extended Pascal (ISO 10206) extension, as well as the possibility of specifying <tt>record</tt> literals.
<syntaxhighlight lang="pascal">type
animalCycle = (rat, ox, tiger, rabbit, dragon, snake,
horse, goat, monkey, rooster, dog, pig);
elementCycle = (wood, fire, earth, metal, water);
aspectCycle = (yang, yin);
zodiac = record
animal: animalCycle;
element: elementCycle;
aspect: aspectCycle;
end;
 
function getZodiac(year: integer): zodiac;
begin
year := pred(year, 4);
getZodiac := zodiac[
animal: succ(rat, year mod 12);
element: succ(wood, year mod 10 div 2);
aspect: succ(yang, year mod 2);
]
end;</syntaxhighlight>
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">sub zodiac {
my $year = shift;
my @animals = qw/Rat Ox Tiger Rabbit Dragon Snake Horse Goat Monkey Rooster Dog Pig/;
Line 1,579 ⟶ 3,102:
 
printf("%4d: %s%s (%s-%s) %s %s; %s - year %d of the cycle\n", zodiac($_))
for (1935, 1938, 1968, 1972, 1976, 2017);</langsyntaxhighlight>
{{out}}
<pre>
Line 1,589 ⟶ 3,112:
2017: 丁酉 (dīng-yŏu) Fire Rooster; yin - year 34 of the cycle
</pre>
 
=={{header|Perl 6}}==
{{works with|Rakudo|2017.01}}
{{trans|Ruby}}
 
<lang perl6>sub Chinese-zodiac ( Int $year ) {
my @heaven = <甲 jiă 乙 yĭ 丙 bĭng 丁 dīng 戊 wù 己 jĭ 庚 gēng 辛 xīn 壬 rén 癸 gŭi>.pairup;
my @earth = <子 zĭ 丑 chŏu 寅 yín 卯 măo 辰 chén 巳 sì 午 wŭ 未 wèi 申 shēn 酉 yŏu 戌 xū 亥 hài>.pairup;
my @animal = <Rat Ox Tiger Rabbit Dragon Snake Horse Goat Monkey Rooster Dog Pig>;
my @element = <Wood Fire Earth Metal Water>;
my @aspect = <yang yin>;
 
my $cycle_year = ($year - 4) % 60;
my $i2 = $cycle_year % 2;
my $i10 = $cycle_year % 10;
my $i12 = $cycle_year % 12;
 
%(
'Han' => @heaven[$i10].key ~ @earth[$i12].key,
'pinyin' => @heaven[$i10].value ~ @earth[$i12].value,
'heaven' => @heaven[$i10],
'earth' => @earth[$i12],
'element' => @element[$i10 div 2],
'animal' => @animal[$i12],
'aspect' => @aspect[$i2],
'cycle' => $cycle_year + 1
)
}
 
# TESTING
printf "%d: %s (%s, %s %s; %s - year %d in the cycle)\n",
$_, .&Chinese-zodiac<Han pinyin element animal aspect cycle>
for 1935, 1938, 1968, 1972, 1976, 1984, Date.today.year;</lang>
 
{{out}}
<pre>1935: 乙亥 (yĭhài, Wood Pig; yin - year 12 in the cycle)
1938: 戊寅 (wùyín, Earth Tiger; yang - year 15 in the cycle)
1968: 戊申 (wùshēn, Earth Monkey; yang - year 45 in the cycle)
1972: 壬子 (rénzĭ, Water Rat; yang - year 49 in the cycle)
1976: 丙辰 (bĭngchén, Fire Dragon; yang - year 53 in the cycle)
1984: 甲子 (jiăzĭ, Wood Rat; yang - year 1 in the cycle)
2017: 丁酉 (dīngyŏu, Fire Rooster; yin - year 34 in the cycle)</pre>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>constant animals = {"Rat","Ox","Tiger","Rabbit","Dragon","Snake","Horse","Goat","Monkey","Rooster","Dog","Pig"},
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
elements = {"Wood","Fire","Earth","Metal","Water"},
<span style="color: #008080;">constant</span> <span style="color: #000000;">animals</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"Rat"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Ox"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Tiger"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Rabbit"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Dragon"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Snake"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Horse"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Goat"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Monkey"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Rooster"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Dog"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Pig"</span><span style="color: #0000FF;">},</span>
yinyang = {"yang","yin"},
<span style="color: #000000;">elements</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"Wood"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Fire"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Earth"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Metal"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Water"</span><span style="color: #0000FF;">},</span>
years = {1935,1938,1968,1972,1976,2018}
<span style="color: #000000;">yinyang</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"yang"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"yin"</span><span style="color: #0000FF;">},</span>
<span style="color: #000000;">years</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1935</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1938</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1968</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1972</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1976</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2018</span><span style="color: #0000FF;">}</span>
for i=1 to length(years) do
integer year = years[i],
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">years</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
cycle = mod(year-4,60)
<span style="color: #004080;">integer</span> <span style="color: #000000;">year</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">years</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span>
string element = elements[floor(mod(cycle,10)/2)+1],
<span style="color: #000000;">cycle</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">year</span><span style="color: #0000FF;">-</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">60</span><span style="color: #0000FF;">)</span>
animal = animals[mod(cycle,12)+1],
<span style="color: #004080;">string</span> <span style="color: #000000;">element</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">elements</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cycle</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span>
yy = yinyang[mod(cycle,2)+1]
<span style="color: #000000;">animal</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">animals</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cycle</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span>
printf(1,"%d: %s %s; %s, year %d of the cycle.\n",
<span style="color: #000000;">yy</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">yinyang</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cycle</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
{year,element,animal,yy,cycle+1})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d: %s %s; %s, year %d of the cycle.\n"</span><span style="color: #0000FF;">,</span>
end for</lang>
<span style="color: #0000FF;">{</span><span style="color: #000000;">year</span><span style="color: #0000FF;">,</span><span style="color: #000000;">element</span><span style="color: #0000FF;">,</span><span style="color: #000000;">animal</span><span style="color: #0000FF;">,</span><span style="color: #000000;">yy</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cycle</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,656 ⟶ 3,139:
2018: Earth Dog; yang, year 35 of the cycle.
</pre>
=={{header|Picat}}==
{{trans|Prolog}}
{{works with|Picat}}
<syntaxhighlight lang="picat">
animals({"Rat", "Ox", "Tiger", "Rabbit", "Dragon", "Snake", "Horse", "Goat", "Monkey", "Rooster", "Dog", "Pig"}).
 
elements({"Wood", "Fire", "Earth", "Metal", "Water"}).
 
animal_chars({"子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"}).
 
element_chars({{"甲", "丙", "戊", "庚", "壬"}, {"乙", "丁", "己", "辛", "癸"}}).
 
years({1935, 1938, 1968, 1972, 1976, 1984, 1985, 2017}).
 
year_animal(Year) = Animal =>
I = ((Year - 4) mod 12) + 1,
animals(Animals),
Animal = Animals[I].
 
year_element(Year) = Element =>
I = ((Year - 4) mod 10) div 2 + 1,
elements(Elements),
Element = Elements[I].
 
year_animal_char(Year) = AnimalChar =>
I = (Year - 4) mod 12 + 1,
animal_chars(AnimalChars),
AnimalChar = AnimalChars[I].
 
year_element_char(Year) = ElementChar =>
I1 = Year mod 2 + 1,
element_chars(ElementChars),
ElementChars1 = ElementChars[I1],
I2 = (Year - 4) mod 10 div 2 + 1,
ElementChar = ElementChars1[I2].
 
year_yin_yang(Year) = YinYang =>
Year mod 2 == 0 -> YinYang = "yang" ; YinYang = "yin".
 
main :-
years(Years),
foreach (Year in Years)
Element = year_element(Year),
Animal = year_animal(Year),
YinYang = year_yin_yang(Year),
ElementChar = year_element_char(Year),
AnimalChar = year_animal_char(Year),
printf("%d is the year of the %w %w (%w). %w%w\n", Year, Element, Animal, YinYang, ElementChar, AnimalChar)
end.
</syntaxhighlight>
{{out}}
<pre>
1935 is the year of the Wood Pig (yin). 乙亥
1938 is the year of the Earth Tiger (yang). 戊寅
1968 is the year of the Earth Monkey (yang). 戊申
1972 is the year of the Water Rat (yang). 壬子
1976 is the year of the Fire Dragon (yang). 丙辰
1984 is the year of the Wood Rat (yang). 甲子
1985 is the year of the Wood Ox (yin). 乙丑
2017 is the year of the Fire Rooster (yin). 丁酉
</pre>
=={{header|PowerShell}}==
{{trans|Ruby}}
<syntaxhighlight lang="powershell">
<lang PowerShell>
function Get-ChineseZodiac
{
Line 1,745 ⟶ 3,288:
}
}
</syntaxhighlight>
</lang>
<syntaxhighlight lang="powershell">
<lang PowerShell>
1935, 1938, 1968, 1972, 1976 | Get-ChineseZodiac | Format-Table
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,760 ⟶ 3,303:
</pre>
Defaults to the current year:
<syntaxhighlight lang="powershell">
<lang PowerShell>
Get-ChineseZodiac
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,774 ⟶ 3,317:
</pre>
Using the '''Year''' property of a <code>[DateTime]</code> object:
<syntaxhighlight lang="powershell">
<lang PowerShell>
Get-Date "11/8/2016" | Get-ChineseZodiac
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,788 ⟶ 3,331:
</pre>
Emulate the Ruby script's output:
<syntaxhighlight lang="powershell">
<lang PowerShell>
$zodiacs = 1935, 1938, 1968, 1972, 1976 | Get-ChineseZodiac
 
Line 1,801 ⟶ 3,344:
$zodiac.YearOfCycle
}
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,810 ⟶ 3,353:
1976: 丙辰 (bĭng-chén, Fire Dragon; yang - year 53 of the cycle)
</pre>
=={{header|Prolog}}==
{{trans|Java}}
{{works with|GNU Prolog}}
<syntaxhighlight lang="prolog">
:- initialization(main).
 
animals(['Rat', 'Ox', 'Tiger', 'Rabbit', 'Dragon', 'Snake', 'Horse', 'Goat', 'Monkey', 'Rooster', 'Dog', 'Pig']).
 
elements(['Wood', 'Fire', 'Earth', 'Metal', 'Water']).
 
animal_chars(['子','丑','寅','卯','辰','巳','午','未','申','酉','戌','亥']).
 
element_chars([['甲', '丙', '戊', '庚', '壬'], ['乙', '丁', '己', '辛', '癸']]).
 
years([1935, 1938, 1968, 1972, 1976, 1984, 1985, 2017]).
 
year_animal(Year, Animal) :-
I is ((Year - 4) mod 12) + 1,
animals(Animals),
nth(I, Animals, Animal).
 
year_element(Year, Element) :-
I is ((Year - 4) mod 10) div 2 + 1,
elements(Elements),
nth(I, Elements, Element).
 
year_animal_char(Year, AnimalChar) :-
I is (Year - 4) mod 12 + 1,
animal_chars(AnimalChars),
nth(I, AnimalChars, AnimalChar).
 
year_element_char(Year, ElementChar) :-
I1 is Year mod 2 + 1,
element_chars(ElementChars),
nth(I1, ElementChars, ElementChars1),
I2 is (Year - 4) mod 10 div 2 + 1,
nth(I2, ElementChars1, ElementChar).
 
year_yin_yang(Year, YinYang) :-
Year mod 2 =:= 0 -> YinYang = 'yang' ; YinYang = 'yin'.
 
main :-
years(Years),
forall(member(Year, Years), (
write(Year),
write(' is the year of the '),
year_element(Year, Element),
write(Element),
write(' '),
year_animal(Year, Animal),
write(Animal),
write(' '),
year_yin_yang(Year, YinYang),
write('('),
write(YinYang),
write('). '),
year_element_char(Year, ElementChar),
write(ElementChar),
year_animal_char(Year, AnimalChar),
write(AnimalChar),
nl
)).
</syntaxhighlight>
{{out}}
<pre>
1935 is the year of the Wood Pig (yin). 乙亥
1938 is the year of the Earth Tiger (yang). 戊寅
1968 is the year of the Earth Monkey (yang). 戊申
1972 is the year of the Water Rat (yang). 壬子
1976 is the year of the Fire Dragon (yang). 丙辰
1984 is the year of the Wood Rat (yang). 甲子
1985 is the year of the Wood Ox (yin). 乙丑
2017 is the year of the Fire Rooster (yin). 丁酉
</pre>
=={{header|PureBasic}}==
<langsyntaxhighlight pureBasiclang="purebasic">EnableExplicit
#BASE=4
#SPC=Chr(32)
Line 1,863 ⟶ 3,479:
Stem: : Data.s "jiă\yĭ\bĭng\dīng\wù\jĭ\gēng\xīn\rén\gŭi"
Branch: : Data.s "zĭ\chŏu\yín\măo\chén\sì\wŭ\wèi\shēn\yŏu\xū\hài"
EndDataSection</langsyntaxhighlight>
{{Out}}
<pre>Year Element Animal Aspect YearOfCycle ASCII Chinese
Line 1,873 ⟶ 3,489:
1984 Wood Rat yang 1 jiă-zĭ 甲子
2017 Fire Rooster yin 34 dīng-yŏu 丁酉</pre>
 
=={{header|Python}}==
{{trans|Ruby}}
<syntaxhighlight lang="python">
<lang Python>
# coding: utf-8
 
Line 1,941 ⟶ 3,556:
years = [1935, 1938, 1968, 1972, 1976, current_year]
for year in years:
calculate(year)</langsyntaxhighlight>
 
 
Or, segmenting tokens just in time, and writing out wiki tables:
{{Works with|Python|3.7}}
<langsyntaxhighlight lang="python">'''Chinese zodiac'''
 
from functools import (reduce)
Line 2,103 ⟶ 3,718:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
{| class="wikitable"
Line 2,250 ⟶ 3,865:
|dark
|}
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ dup echo
say " is the year of the "
4 - dup 10 mod 2 /
[ table $ "Wood" $ "Fire" $ "Earth"
$ "Metal" $ "Water" ]
do echo$
sp
dup 12 mod
[ table
$ "Rat" $ "Ox" $ "Tiger" $ "Rabbit"
$ "Dragon" $ "Snake" $ "Horse" $ "Goat"
$ "Monkey" $ "Rooster" $ "Dog" $ "Pig" ]
do echo$
say " ("
2 mod
[ table $ "yang" $ "yin" ]
do echo$
say ")." cr ] is zodiac ( $ --> )
 
' [ 1935 1938 1968 1972 1976 1984 1985 2017 ]
witheach zodiac</syntaxhighlight>
 
{{out}}
 
<pre>1935 is the year of the Wood Pig (yin).
1938 is the year of the Earth Tiger (yang).
1968 is the year of the Earth Monkey (yang).
1972 is the year of the Water Rat (yang).
1976 is the year of the Fire Dragon (yang).
1984 is the year of the Wood Rat (yang).
1985 is the year of the Wood Ox (yin).
2017 is the year of the Fire Rooster (yin).
</pre>
 
=={{header|Racket}}==
{{trans|Common Lisp}}
 
<langsyntaxhighlight lang="racket">#lang racket
 
(require racket/date)
Line 2,299 ⟶ 3,950:
(module+ test
(for ((ce-year (in-list '(1935 1938 1941 1947 1968 1972 1976))))
(apply printf "~a: ~a~a (~a-~a, ~a ~a; ~a)~%" (chinese-zodiac ce-year))))</langsyntaxhighlight>
 
{{out}}
Line 2,309 ⟶ 3,960:
1972: 壬子 (rén-zĭ, Water Rat; yang)
1976: 丙辰 (bĭng-chén, Fire Dragon; yang)</pre>
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2017.01}}
{{trans|Ruby}}
 
<syntaxhighlight lang="raku" line>sub Chinese-zodiac ( Int $year ) {
my @heaven = <甲 jiă 乙 yĭ 丙 bĭng 丁 dīng 戊 wù 己 jĭ 庚 gēng 辛 xīn 壬 rén 癸 gŭi>.pairup;
my @earth = <子 zĭ 丑 chŏu 寅 yín 卯 măo 辰 chén 巳 sì 午 wŭ 未 wèi 申 shēn 酉 yŏu 戌 xū 亥 hài>.pairup;
my @animal = <Rat Ox Tiger Rabbit Dragon Snake Horse Goat Monkey Rooster Dog Pig>;
my @element = <Wood Fire Earth Metal Water>;
my @aspect = <yang yin>;
 
my $cycle_year = ($year - 4) % 60;
my $i2 = $cycle_year % 2;
my $i10 = $cycle_year % 10;
my $i12 = $cycle_year % 12;
 
%(
'Han' => @heaven[$i10].key ~ @earth[$i12].key,
'pinyin' => @heaven[$i10].value ~ @earth[$i12].value,
'heaven' => @heaven[$i10],
'earth' => @earth[$i12],
'element' => @element[$i10 div 2],
'animal' => @animal[$i12],
'aspect' => @aspect[$i2],
'cycle' => $cycle_year + 1
)
}
 
# TESTING
printf "%d: %s (%s, %s %s; %s - year %d in the cycle)\n",
$_, .&Chinese-zodiac<Han pinyin element animal aspect cycle>
for 1935, 1938, 1968, 1972, 1976, 1984, Date.today.year;</syntaxhighlight>
 
{{out}}
<pre>1935: 乙亥 (yĭhài, Wood Pig; yin - year 12 in the cycle)
1938: 戊寅 (wùyín, Earth Tiger; yang - year 15 in the cycle)
1968: 戊申 (wùshēn, Earth Monkey; yang - year 45 in the cycle)
1972: 壬子 (rénzĭ, Water Rat; yang - year 49 in the cycle)
1976: 丙辰 (bĭngchén, Fire Dragon; yang - year 53 in the cycle)
1984: 甲子 (jiăzĭ, Wood Rat; yang - year 1 in the cycle)
2017: 丁酉 (dīngyŏu, Fire Rooster; yin - year 34 in the cycle)</pre>
=={{header|Ring}}==
<syntaxhighlight lang="ring">
yinyang = ["yang", "yin"]
elements = ["Wood", "Fire", "Earth", "Metal", "Water"]
animals = ["Rat", "Ox", "Tiger", "Rabbit", "Dragon", "Snake",
"Horse", "Goat", "Monkey", "Rooster", "Dog", "Pig"]
years = [1801, 1861, 1984, 2020, 2186, 76543]
output = ""
for year in years
yy = year % 2 + 1
element = (year - 4) % 5 + 1
animal = (year - 4) % 12 + 1
output = string(year) + " is the year of the "
output += elements[element] + " " + animals[animal] + " (" + yinyang[yy] + ")."
? output
next
</syntaxhighlight>
{{out}}
<pre>
1801 is the year of the Earth Rooster (yin).
1861 is the year of the Earth Rooster (yin).
1984 is the year of the Wood Rat (yang).
2020 is the year of the Fire Rat (yang).
2186 is the year of the Earth Dog (yang).
76543 is the year of the Water Rabbit (yin).
</pre>
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
≪ 4 -
{ "Wood " "Fire " "Earth " "Metal " "Water " }
OVER 10 MOD 1 + 2 / GET
{ "Rat" "Ox" "Tiger" "Rabbit" "Dragon" "Snake" "Horse" "Goat" "Monkey" "Rooster" "Dog" "Pig" }
3 PICK 12 MOD 1 + GET +
SWAP 2 MOD " (yin)" " (yang)" IFTE +
≫ ''''CHZOD'''' STO
 
2022 '''CHZOD'''
{{out}}
<pre>
1: "Water Tiger (yang)"
</pre>
 
=={{header|Ruby}}==
This is written as a command-line tool that takes a list of CE year numbers as arguments and outputs their information; if no arguments are supplied, it displays the information for the current year.
 
<langsyntaxhighlight lang="ruby"># encoding: utf-8
pinyin = {
'甲' => 'jiă',
Line 2,378 ⟶ 4,113:
print stem_han, branch_han
puts " (#{stem_pinyin}-#{branch_pinyin}, #{element} #{animal}; #{aspect} - year #{index} of the cycle)"
end</langsyntaxhighlight>
 
{{Output}}
Line 2,393 ⟶ 4,128:
=={{header|Rust}}==
{{trans|Kotlin}}
<langsyntaxhighlight Rustlang="rust">fn chinese_zodiac(year: usize) -> String {
static ANIMALS: [&str; 12] = [
"Rat", "Ox", "Tiger", "Rabbit", "Dragon", "Snake",
Line 2,447 ⟶ 4,182:
println!("{}", chinese_zodiac(year));
}
}</langsyntaxhighlight>
{{out}}
<pre>Year Chinese Pinyin Element Animal Aspect Cycle
Line 2,458 ⟶ 4,193:
1984 甲子 jiă-zĭ Wood Rat Yang 01/60
2017 丁酉 dīng-yŏu Fire Rooster Yin 34/60</pre>
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">object Zodiac extends App {
val years = Seq(1935, 1938, 1968, 1972, 1976, 1984, 1985, 2017, 2018)
 
Line 2,495 ⟶ 4,229:
+ animalChars((year - 4) % 12))
}
}</langsyntaxhighlight>
{{Out}}See it in running in your browser by [https://scalafiddle.io/sf/O6MUr27/0 ScalaFiddle (JavaScript)] or by [https://scastie.scala-lang.org/KXC0j71ORFaQxNZSCCZ1Aw Scastie (JVM)].
 
=={{header|Seed7}}==
The standard output file of Seed7 accepts only Latin-1 (byte) output.
Line 2,504 ⟶ 4,237:
The library [http://seed7.sourceforge.net/libraries/console.htm console.s7i] defines STD_CONSOLE.
 
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "console.s7i";
 
Line 2,525 ⟶ 4,258:
([0] ("yang", "yin"))[year rem 2] <& "). " <& elementChars[year rem 2][eIdx] <& animalChars[aIdx]);
end for;
end func;</langsyntaxhighlight>
 
{{out}}
Line 2,538 ⟶ 4,271:
2017 is the year of the Fire Rooster (yin). 丁酉
</pre>
 
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">func zodiac(year) {
var animals = %w(Rat Ox Tiger Rabbit Dragon Snake Horse Goat Monkey Rooster Dog Pig)
var elements = %w(Wood Fire Earth Metal Water)
Line 2,561 ⟶ 4,293:
[1935, 1938, 1968, 1972, 1976, 2017].each { |year|
printf("%4d: %s%s (%s-%s) %s %s; %s - year %d of the cycle\n", zodiac(year))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,571 ⟶ 4,303:
2017: 丁酉 (dīng-yŏu) Fire Rooster; yin - year 34 of the cycle
</pre>
 
=={{header|tbas}}==
<langsyntaxhighlight lang="qbasic">
DATA "甲","乙","丙","丁","戊","己","庚","辛","壬","癸"
DECLARE celestial$(10)
Line 2,644 ⟶ 4,375:
PRINT TAB(50);"year";index;"of the cycle"
NEXT
</syntaxhighlight>
</lang>
<pre>
$ tbas chinZod.bas
Line 2,659 ⟶ 4,390:
</pre>
=={{header|Tcl}}==
<syntaxhighlight lang="tcl">
<lang Tcl>
proc cn_zodiac year {
set year0 [expr $year-4]
Line 2,677 ⟶ 4,408:
return $res
}
</langsyntaxhighlight><pre>
% cn_zodiac 1984
甲子 jia3-zi3 Wood Rat (yang) year 1
Line 2,683 ⟶ 4,414:
丁酉 ding1-you3 Fire Rooster (yin) year 34
</pre>
=={{header|uBasic/4tH}}==
{{works with|R3}}
<syntaxhighlight lang="text">dim @y(2) ' yin or yang
dim @e(5) ' the elements
dim @a(12) ' the animals
 
Push "yang", "yin" ' fill Ying/Yang table
For i = 1 to 0 Step -1 : @y(i) = Pop() : Next i
' fill Elements table
Push "Wood", "Fire", "Earth", "Metal", "Water"
For i = 4 to 0 Step -1 : @e(i) = Pop() : Next i
' fill Animals table
Push "Rat", "Ox", "Tiger", "Rabbit", "Dragon", "Snake"
Push "Horse", "Goat", "Monkey", "Rooster", "Dog", "Pig"
 
For i = 11 to 0 step -1 : @a(i) = Pop() : Next i
' now push all the test years
Push 76543, 2186, 2020, 1984, 1861, 1801
' and process them all
Do While Used() ' until the stack is empty
Print Show(FUNC(_Chinese(Pop()))) ' call function
Loop
 
End
_Chinese ' compose string
Param (1)
Local (3)
b@ = a@ % 2
c@ = (a@ - 4) % 5
d@ = (a@ - 4) % 12
Return (Join(Str (a@), " is the year of the ", @e(c@), " ", @a(d@), " (", @y(b@), ")."))
</syntaxhighlight>
Output:
<pre>
1801 is the year of the Earth Rooster (yin).
1861 is the year of the Earth Rooster (yin).
1984 is the year of the Wood Rat (yang).
2020 is the year of the Fire Rat (yang).
2186 is the year of the Earth Dog (yang).
76543 is the year of the Water Rabbit (yin).
 
0 OK, 0:1047
</pre>
 
=={{header|UNIX Shell}}==
{{works with|Bash|4+}}
{{works with|Korn Shell|93+}}
 
The required versions introduced support for associative arrays.
 
<pre>#!/usr/bin/env bash
declare -A pinyin=(
[甲]='jiă'
[乙]='yĭ'
[丙]='bĭng'
[丁]='dīng'
[戊]='wù'
[己]='jĭ'
[庚]='gēng'
[辛]='xīn'
[壬]='rén'
[癸]='gŭi'
 
[子]='zĭ'
[丑]='chŏu'
[寅]='yín'
[卯]='măo'
[辰]='chén'
[巳]='sì'
[午]='wŭ'
[未]='wèi'
[申]='shén'
[酉]='yŏu'
[戌]='xū'
[亥]='hài'
)
 
celestial=(甲 乙 丙 丁 戊 己 庚 辛 壬 癸)
terrestrial=(子 丑 寅 卯 辰 巳 午 未 申 酉 戌 亥)
animals=(Rat Ox Tiger Rabbit Dragon Snake
Horse Goat Monkey Rooster Dog Pig)
elements=(Wood Fire Earth Metal Water)
aspects=(yang yin)
 
BaseYear=4
 
function main {
if (( !$# )); then
set -- $(date +%Y)
fi
local year
for year; do
if (( $# > 1 )); then
printf '%s:' "$year"
fi
local -i cycle_year=year-BaseYear
local -i stem_number=$cycle_year%${#celestial[@]}
local stem_han=${celestial[$stem_number]}
local stem_pinyin=${pinyin[$stem_han]}
 
local -i element_number=stem_number/2
local element=${elements[$element_number]}
 
local -i branch_number=$cycle_year%${#terrestrial[@]}
local branch_han=${terrestrial[$branch_number]}
local branch_pinyin=${pinyin[$branch_han]}
local animal=${animals[$branch_number]}
 
local -i aspect_number=$cycle_year%${#aspects[@]}
local aspect=${aspects[$aspect_number]}
 
printf '%s%s' $stem_han $branch_han
printf '(%s-%s, %s %s; %s)\n' $stem_pinyin $branch_pinyin $element $animal $aspect
done
}
 
main "$@"</pre>
 
{{works with|Z Shell|5.5+}}
Version required for associative array initialization syntax.
Main difference from bash is that we number everything starting at 1 instead of 0.
 
<pre>#!/usr/bin/env zsh
declare -A pinyin=(
[甲]='jiă'
[乙]='yĭ'
[丙]='bĭng'
[丁]='dīng'
[戊]='wù'
[己]='jĭ'
[庚]='gēng'
[辛]='xīn'
[壬]='rén'
[癸]='gŭi'
 
[子]='zĭ'
[丑]='chŏu'
[寅]='yín'
[卯]='măo'
[辰]='chén'
[巳]='sì'
[午]='wŭ'
[未]='wèi'
[申]='shén'
[酉]='yŏu'
[戌]='xū'
[亥]='hài'
)
 
celestial=(甲 乙 丙 丁 戊 己 庚 辛 壬 癸)
terrestrial=(子 丑 寅 卯 辰 巳 午 未 申 酉 戌 亥)
animals=(Rat Ox Tiger Rabbit Dragon Snake
Horse Goat Monkey Rooster Dog Pig)
elements=(Wood Fire Earth Metal Water)
aspects=(yang yin)
 
BaseYear=4
 
main() {
if (( !$# )); then
set -- $(date +%Y)
fi
local year
for year; do
if (( $# > 1 )); then
printf '%s:' "$year"
fi
local -i cycle_year=year-BaseYear+1
local -i stem_number=$(amod $cycle_year ${#celestial[@]})
local stem_han=$celestial[$stem_number]
local stem_pinyin=$pinyin[$stem_han]
 
local -i element_number
(( element_number = (stem_number-1)/2+1 ))
local element=$elements[$element_number]
 
local -i branch_number=$(amod $cycle_year ${#terrestrial[@]})
local branch_han=$terrestrial[$branch_number]
local branch_pinyin=$pinyin[$branch_han]
local animal=$animals[$branch_number]
 
local -i aspect_number=$(amod $cycle_year ${#aspects[@]})
local aspect=$aspects[$aspect_number]
 
printf '%s%s' $stem_han $branch_han
printf '(%s-%s, %s %s; %s)\n' $stem_pinyin $branch_pinyin $element $animal $aspect
done
}
 
amod() {
local -i m=$1 n=$2
printf '%d\n' $(( (m + n - 1) % n + 1 ))
}
 
main "$@"</pre>
 
{{Out}}
Output is the same for both versions:
<pre>$ cz 1935 1938 1968 1972 1976:
1935:乙亥(yĭ-hài, Wood Pig; yin)
1938:戊寅(wù-yín, Earth Tiger; yang)
1968:戊申(wù-shén, Earth Monkey; yang)
1972:壬子(rén-zĭ, Water Rat; yang)
1976:丙辰(bĭng-chén, Fire Dragon; yang)</pre>
=={{header|UTFool}}==
 
<syntaxhighlight lang="utfool">
<lang UTFool>
···
http://rosettacode.org/wiki/Chinese_zodiac
Line 2,726 ⟶ 4,662:
System.out.printf " %-2s/60 %-7s %s\n\n", cycle \ 60 + 1,
wuxing[2][stem / 2], shengxiao[2][branch]
</syntaxhighlight>
</lang>
 
=={{header|VBScript}}==
To run in console mode with cscript.
<langsyntaxhighlight lang="vb">' Chinese zodiac - VBS
 
Animals = array( "Rat","Ox","Tiger","Rabbit","Dragon","Snake","Horse","Goat","Monkey","Rooster","Dog","Pig" )
Line 2,745 ⟶ 4,680:
msgbox xYear & " is the year of the " & yElement & " " & yAnimal & " (" & yYinYang & ").",, _
xYear & " : " & nn & "/60"
next </langsyntaxhighlight>
{{out}}
<pre>
Line 2,756 ⟶ 4,691:
2017 is the year of the Fire Rooster (Yin).
</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Module Module1
 
ReadOnly ANIMALS As String() = {"Rat", "Ox", "Tiger", "Rabbit", "Dragon", "Snake", "Horse", "Goat", "Monkey", "Rooster", "Dog", "Pig"}
Line 2,789 ⟶ 4,723:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>1935 is the year of the Wood Pig (yin). 乙亥
Line 2,799 ⟶ 4,733:
1985 is the year of the Wood Ox (yin). 乙丑
2017 is the year of the Fire Rooster (yin). 丁酉</pre>
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">const (
animal_string = ["Rat", "Ox", "Tiger", "Rabbit", "Dragon", "Snake",
"Horse", "Goat", "Monkey", "Rooster", "Dog", "Pig"]
stem_yy_string = ["Yang", "Yin"]
element_string = ["Wood", "Fire", "Earth", "Metal", "Water"]
stem_ch = "甲乙丙丁戊己庚辛壬癸".split('')
branch_ch = "子丑寅卯辰巳午未申酉戌亥".split('')
)
fn cz(y int) (string, string, string, string, int) {
yr := y-4
stem := yr % 10
branch := yr % 12
return animal_string[branch],
stem_yy_string[stem%2],
element_string[stem/2],
[stem_ch[stem], branch_ch[branch]].join(''),
yr%60 + 1
}
fn main() {
for yr in [1935, 1938, 1968, 1972, 1976] {
a, yy, e, sb, cy := cz(yr)
println("$yr: $e $a, $yy, Cycle year $cy $sb")
}
}</syntaxhighlight>
 
{{out}}
<pre>
Same as Go entry
</pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./fmt" for Fmt
 
class ChineseZodiac {
static init() {
__animals = ["Rat", "Ox", "Tiger", "Rabbit", "Dragon", "Snake",
"Horse", "Goat", "Monkey", "Rooster", "Dog", "Pig"]
__aspects = ["Yang","Yin"]
__elements = ["Wood", "Fire", "Earth", "Metal", "Water"]
__stems = ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"]
__branches = ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"]
__sNames = ["jiă", "yĭ", "bĭng", "dīng", "wù", "jĭ", "gēng", "xīn", "rén", "gŭi"]
__bNames = ["zĭ", "chŏu", "yín", "măo", "chén", "sì", "wŭ", "wèi", "shēn", "yŏu", "xū", "hài"]
}
 
construct new(year) {
var y = year - 4
var s = y % 10
var b = y % 12
_year = year
_stem = __stems[s]
_branch = __branches[b]
_sName = __sNames[s]
_bName = __bNames[b]
_element = __elements[(s/2).floor]
_animal = __animals[b]
_aspect = __aspects[s % 2]
_cycle = y % 60 + 1
}
 
toString {
var name = Fmt.s(-9, _sName + "-" + _bName)
var elem = Fmt.s(-7, _element)
var anim = Fmt.s(-7, _animal)
var aspt = Fmt.s(-6, _aspect)
var cycl = Fmt.dz(2, _cycle) + "/60"
return "%(_year) %(_stem)%(_branch) %(name) %(elem) %(anim) %(aspt) %(cycl)"
}
}
 
var years = [1935, 1938, 1968, 1972, 1976, 1984, 2017, 2020]
System.print("Year Chinese Pinyin Element Animal Aspect Cycle")
System.print("---- ------- --------- ------- ------- ------ -----")
ChineseZodiac.init()
for (year in years) System.print(ChineseZodiac.new(year))</syntaxhighlight>
 
{{out}}
<pre>
Year Chinese Pinyin Element Animal Aspect Cycle
---- ------- --------- ------- ------- ------ -----
1935 乙亥 yĭ-hài Wood Pig Yin 12/60
1938 戊寅 wù-yín Earth Tiger Yang 15/60
1968 戊申 wù-shēn Earth Monkey Yang 45/60
1972 壬子 rén-zĭ Water Rat Yang 49/60
1976 丙辰 bĭng-chén Fire Dragon Yang 53/60
1984 甲子 jiă-zĭ Wood Rat Yang 01/60
2017 丁酉 dīng-yŏu Fire Rooster Yin 34/60
2020 庚子 gēng-zĭ Metal Rat Yang 37/60
</pre>
 
=={{header|XPL0}}==
{{trans|C}}
<syntaxhighlight lang "XPL0">include xpllib; \for Print
 
int Animals, Elements;
 
func GetElement(Year);
int Year, Element;
[Element:= rem((Year-4)/10) / 2;
return Elements(Element);
];
 
func GetAnimal(Year);
int Year;
return Animals(rem((Year-4)/12));
 
func GetYY(Year);
int Year;
if rem(Year/2) = 0 then return "yang"
else return "yin";
 
int Year, Years, I;
[Years:= [ 1935, 1938, 1968, 1972, 1976, 2017 ];
Animals:= [ "Rat","Ox","Tiger","Rabbit","Dragon","Snake","Horse","Goat","Monkey","Rooster","Dog","Pig" ];
Elements:= [ "Wood","Fire","Earth","Metal","Water" ];
for I:= 0 to 6-1 do
[Year:= Years(I);
Print("%d is the year of the %s %s (%s).\n", Year, GetElement(Year), GetAnimal(Year), GetYY(Year));
]
]</syntaxhighlight>
{{out}}
<pre>
1935 is the year of the Wood Pig (yin).
1938 is the year of the Earth Tiger (yang).
1968 is the year of the Earth Monkey (yang).
1972 is the year of the Water Rat (yang).
1976 is the year of the Fire Dragon (yang).
2017 is the year of the Fire Rooster (yin).
</pre>
 
=={{header|Yabasic}}==
<syntaxhighlight lang="yabasic">
dim Animals$(12) : for a = 0 to 11 : read Animals$(a) : next a
dim Elements$(5) : for a = 0 to 4 : read Elements$(a): next a
dim YinYang$(2) : for a = 0 to 1 : read YinYang$(a) : next a
dim Years(7) : for a = 0 to 6 : read Years(a) : next a
 
for i = 0 to arraysize(Years(),1)-1
xYear = Years(i)
yElement$ = Elements$((Mod((xYear - 4), 10)) / 2)
yAnimal$ = Animals$( Mod((xYear - 4), 12))
yYinYang$ = YinYang$( Mod(xYear, 2) )
nn = ( Mod((xYear - 4), 60)) + 1
print xYear, " is the year of the ", yElement$, " ", yAnimal$, " (", yYinYang$, ")."
next i
print
end
 
data "Rat","Ox","Tiger","Rabbit","Dragon","Snake","Horse","Goat","Monkey","Rooster","Dog","Pig"
data "Wood","Fire","Earth","Metal","Water"
data "Yang","Yin"
data 1935, 1938, 1968, 1972, 1976, 1984, 2017
</syntaxhighlight>
{{out}}
<pre>
1935 is the year of the Wood Pig (Yin).
1938 is the year of the Earth Tiger (Yang).
1968 is the year of the Earth Monkey (Yang).
1972 is the year of the Water Rat (Yang).
1976 is the year of the Fire Dragon (Yang).
1984 is the year of the Wood Rat (Yang).
2017 is the year of the Fire Rooster (Yin).
</pre>
=={{header|zkl}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="zkl">fcn ceToChineseZodiac(ce_year){ // --> list of strings
# encoding: utf-8
var [const] pinyin=SD( // create some static variables, SD is small fixed dictionary
Line 2,833 ⟶ 4,935:
return(stem_han,branch_han,
stem_pinyin,branch_pinyin, element,animal,aspect)
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">foreach ce_year in (T(1935,1938,1968,1972,1976,Time.Clock.UTC[0])){
println("%d: %s%s (%s-%s, %s %s; %s)"
.fmt(ce_year,ceToChineseZodiac(ce_year).xplode()));
}</langsyntaxhighlight>
{{out}}
<pre>
1,481

edits