Open in App
Not now

# Program to calculate Dooms Day for a year

• Difficulty Level : Hard
• Last Updated : 21 Jul, 2022

Doomsday may refer to a hypothetical event according to which the end of human life is at the highest possibility. There are many algorithms written to calculate which day of a week in a year has the highest possibility of doomsday falling on that day.
All the statements are with respect to Gregorian Calendar. As the Gregorian calendar repeats itself every 400 years so a set of rules is decided for 1st 400 years only. Algorithms are derived from the calculations of John Conway, Lewis Carroll and many other mathematicians in history who worked on the calculation of Dooms Day.
To calculate Doom’s Day of a particular year following algorithm is used:-

• Extract the last two digits of the year.(Let this be y)
• Divide by 12 take the floor of the value.
• Then add remainder of dividing y by 12.
• Calculate the result when remainder of y divided by 12 is divided by 4.
• Take the floor of the above value and then add.
• Take remainder after dividing with 7 (mod 7).
• Add the value of anchor day of the week starting from Sunday (Considering Sunday as 0)

The formula becomes –

Here [ ] is Greatest Integer Function.
Anchor day changes after 100 years and repeats after every 400 years in the following way –

```0-99 yrs --> Tuesday
100-199 yrs --> Sunday
200-299 yrs --> Friday
300-399 yr --> Wednesday```

After this the above anchor days repeat as mentioned in the beginning of the article.
Examples:

```Input :  2005
Output : Doomsday in the year 2005 = Monday

Input : 1800
Output : Doomsday in the year 1800 = Friday```

Below is the implementation.

## C++14

 `#include` `using` `namespace` `std;`   `string dooms_day(``int` `year)` `{` `    `  `    ``// map to store days value of ` `    ``// anchor day can be known ` `    ``map<``int``, string> dict_day;` `    ``dict_day[0] = ``"Sunday"``;` `    ``dict_day[1] = ``"Monday"``;` `    ``dict_day[2] = ``"Tuesday"``;` `    ``dict_day[3] = ``"Wednesday"``;` `    ``dict_day[4] = ``"Thursday"``;` `    ``dict_day[5] = ``"Friday"``;` `    ``dict_day[6] = ``"Saturday"``;` `    `  `    ``// Gregorian calendar repeats ` `    ``// every 400 years ` `    ``int` `k = year % 400;` `    `  `    ``int` `anchor;` `    `  `    ``// Decide the anchor day ` `    ``if``(k >= 0 && k < 100)` `        ``anchor = 2;` `        `  `    ``else` `if``(k >= 100 && k < 200)` `        ``anchor = 0;` `        `  `    ``else` `if``(k >= 200 && k < 300)` `        ``anchor = 5;` `        `  `    ``else` `        ``anchor = 3;` `        `  `    ``int` `y = year % 100;` `    `  `    ``// Dooms day formula by Conway ` `    ``int` `doomsday = ((y / 12 + y % 12 + ` `                    ``(y % 12) / 4) % 7 + anchor) % 7;` `    `  `    ``return` `dict_day[doomsday];` `}`   `// Driver code ` `int` `main()` `{` `    ``int` `year = 1966;` `    `  `    ``cout << ``"Doomsday in the year "` `         ``<< year << ``" = "` `<< dooms_day(year);` `    `  `    ``return` `0;` `}`   `// This code is contributed by yatinagg`

## Java

 `import` `java.util.*;`   `class` `GFG{` `    `  `public` `static` `String dooms_day(``int` `year) ` `{ ` `    `  `    ``// map to store days value of  ` `    ``// anchor day can be known  ` `    ``HashMap dict_day = ``new` `HashMap<>(); ` `    ``dict_day.put(``0``, ``"Sunday"``); ` `    ``dict_day.put(``1``, ``"Monday"``); ` `    ``dict_day.put(``2``, ``"Tuesday"``); ` `    ``dict_day.put(``3``, ``"Wednesday"``); ` `    ``dict_day.put(``4``, ``"Thursday"``); ` `    ``dict_day.put(``5``, ``"Friday"``); ` `    ``dict_day.put(``6``, ``"Saturday"``); ` `      `  `    ``// Gregorian calendar repeats  ` `    ``// every 400 years  ` `    ``int` `k = year % ``400``; ` `      `  `    ``int` `anchor; ` `      `  `    ``// Decide the anchor day  ` `    ``if` `(k >= ``0` `&& k < ``100``) ` `        ``anchor = ``2``; ` `          `  `    ``else` `if` `(k >= ``100` `&& k < ``200``) ` `        ``anchor = ``0``; ` `          `  `    ``else` `if` `(k >= ``200` `&& k < ``300``) ` `        ``anchor = ``5``; ` `          `  `    ``else` `        ``anchor = ``3``; ` `          `  `    ``int` `y = year % ``100``; ` `      `  `    ``// Dooms day formula by Conway  ` `    ``int` `doomsday = ((y / ``12` `+ y % ``12` `+  ` `                   ``(y % ``12``) / ``4``) % ``7` `+` `                    ``anchor) % ``7``; ` `      `  `    ``return` `dict_day.get(doomsday); ` `} `   `// Driver code` `public` `static` `void` `main(String[] args)` `{` `    ``int` `year = ``1966``; ` `    `  `    ``System.out.println(``"Doomsday in the year "` `+` `                        ``year + ``" = "` `+ ` `                        ``dooms_day(year));` `}` `}`   `// This code is contributed divyeshrabadiya07`

## Python3

 `def` `dooms_day(year):` `    `  `    ``# dictionary to store days` `    ``# value of anchor day can be known` `    ``dict_day ``=``{ ``0` `: ``"Sunday"``,` `               ``1` `: ``"Monday"``,` `               ``2` `: ``"Tuesday"``,` `               ``3` `: ``"Wednesday"``,` `               ``4` `: ``"Thursday"``,` `               ``5` `: ``"Friday"``,` `               ``6` `: ``"Saturday"` `} ` `    `  `    ``# gregorian calendar repeats` `    ``# every 400 years` `    ``k ``=` `year ``%` `400` `    `  `    ``# decide the anchor day` `    ``if``(k >``=` `0` `and` `k < ``100``):` `        ``anchor ``=` `2` `        `  `    ``elif``(k >``=` `100` `and` `k < ``200``):` `        ``anchor ``=` `0` `        `  `    ``elif``(k >``=` `200` `and` `k < ``300``):` `        ``anchor ``=` `5` `        `  `    ``else``:` `        ``anchor ``=` `3` `        `  `    ``y ``=` `year ``%` `100` `    `  `    ``# dooms day formula by Conway` `    ``doomsday ``=` `((y``/``/``12` `+` `y ``%` `12` `+` `(y ``%` `12``)``/``/``4``)``%` `7` `+` `anchor) ``%` `7` `    `  `    ``return` `dict_day[doomsday]`   `# Driver code` `year ``=` `1966` `print``(``"Doomsday in the year % s = % s"``%``(year, ` `                                        ``dooms_day(year)))`

## C#

 `using` `System;` `using` `System.Collections.Generic;   ` `class` `GFG` `{   ` `    ``static` `String dooms_day(``int` `year) ` `    ``{ ` `         `  `        ``// map to store days value of  ` `        ``// anchor day can be known  ` `        ``Dictionary<``int``, ``string``> dict_day =   ` `                       ``new` `Dictionary<``int``, ``string``>();  ` `        ``dict_day.Add(0, ``"Sunday"``); ` `        ``dict_day.Add(1, ``"Monday"``); ` `        ``dict_day.Add(2, ``"Tuesday"``); ` `        ``dict_day.Add(3, ``"Wednesday"``); ` `        ``dict_day.Add(4, ``"Thursday"``); ` `        ``dict_day.Add(5, ``"Friday"``); ` `        ``dict_day.Add(6, ``"Saturday"``); ` `           `  `        ``// Gregorian calendar repeats  ` `        ``// every 400 years  ` `        ``int` `k = year % 400; ` `           `  `        ``int` `anchor; ` `           `  `        ``// Decide the anchor day  ` `        ``if` `(k >= 0 && k < 100) ` `            ``anchor = 2; ` `               `  `        ``else` `if` `(k >= 100 && k < 200) ` `            ``anchor = 0; ` `               `  `        ``else` `if` `(k >= 200 && k < 300) ` `            ``anchor = 5; ` `               `  `        ``else` `            ``anchor = 3; ` `               `  `        ``int` `y = year % 100; ` `           `  `        ``// Dooms day formula by Conway  ` `        ``int` `doomsday = ((y / 12 + y % 12 +  ` `                       ``(y % 12) / 4) % 7 +` `                        ``anchor) % 7; ` `           `  `        ``return` `dict_day[doomsday]; ` `    ``} ` `  `  `  ``// Driver code` `  ``static` `void` `Main()` `  ``{` `        ``int` `year = 1966; ` `     `  `        ``Console.WriteLine(``"Doomsday in the year "` `+` `                            ``year + ``" = "` `+ ` `                            ``dooms_day(year));` `  ``}` `}`   `// This code is contributed by divyesh072019`

## Javascript

 ``

Output:

`Doomsday in the year 1966 = Monday`

Time Complexity: – O (1)

Space Complexity: – O (1)

My Personal Notes arrow_drop_up
Related Articles