Related Articles

# Find the number of times every day occurs in a month

• Difficulty Level : Basic
• Last Updated : 08 Apr, 2021

Given the starting day and the number of days in a month. Find the number of times every day occurs in a month
Examples:

```Input : Number of days in month = 28
First day = Wednesday
Output : Monday = 4
Tuesday = 4
Wednesday = 4
Thursday = 4
Friday = 4
Saturday = 4
Sunday = 4
Explanation: In the month of February,
every day occurs 4 times.

Input : Number of days in  month = 31
First day = Tuesday
Output : Monday = 4
Tuesday = 5
Wednesday = 5
Thursday = 5
Friday = 4
Saturday = 4
Sunday = 4
Explanation: The month starts on Tuesday
and ends on Thursday. ```

Observations: We have to make some key observations. First one will be if the month has 28 days then every day occurs 4 times. The second one will be if it has 29 days then the day on which the month starts will occur 5 times. The third one will be if the month has 30 days, then the day on which the month starts and the next day will occur 5 days. The last one is if the month has 31 days, then the day on which the month starts and the next 2 days will occur 5 days with the rest occurring 4 times each.
Approach: Create an count array with size 7 and having an initial value of 4 as the minimum number of occurrence will be 4. (number of days-28). Find the index of the firstday. Calculate the number of days whose occurrence will be 5. Then run a loop from pos to pos+(number of days-28) to mark the occurrence as 5. If pos+(number of days-28) exceeds 6, then use %7 to get to the indexes from the beginning.
Below is the C++ implementation of the above approach:

## CPP

 `// C++ program to count occurrence of days in a month``#include ``using` `namespace` `std;` `// function to find occurrences``void` `occurrenceDays(``int` `n, string firstday)``{``    ``// stores days in a week``    ``string days[] = { ``"Monday"``, ``"Tuesday"``, ``"Wednesday"``,``           ``"Thursday"``,  ``"Friday"``, ``"Saturday"``, ``"Sunday"` `};` `    ``// Initialize all counts as 4.``    ``int` `count;``    ``for` `(``int` `i = 0; i < 7; i++)``        ``count[i] = 4;` `    ``// find index of the first day``    ``int` `pos;``    ``for` `(``int` `i = 0; i < 7; i++) {``        ``if` `(firstday == days[i]) {``            ``pos = i;``            ``break``;``        ``}``    ``}` `    ``// number of days whose occurrence will be 5``    ``int` `inc = n - 28;` `    ``// mark the occurrence to be 5 of n-28 days``    ``for` `(``int` `i = pos; i < pos + inc; i++) {``        ``if` `(i > 6)``            ``count[i % 7] = 5;``        ``else``            ``count[i] = 5;``    ``}` `    ``// print the days``    ``for` `(``int` `i = 0; i < 7; i++) {``        ``cout << days[i] << ``" "` `<< count[i] << endl;``    ``}``}` `// driver program to test the above function``int` `main()``{``    ``int` `n = 31;``    ``string firstday = ``"Tuesday"``;``    ``occurrenceDays(n, firstday);``    ``return` `0;``}`

## Java

 `// Java program to count``// occurrence of days in a month``import` `java.util.*;``import` `java.lang.*;` `public` `class` `GfG{` `    ``// function to find occurrences``    ``public` `static` `void` `occurrenceDays(``int` `n, String firstday)``    ``{``        ``// stores days in a week``        ``String[] days = ``new` `String[]{ ``"Monday"``,``                ``"Tuesday"``, ``"Wednesday"``,``                ``"Thursday"``, ``"Friday"``,``                ``"Saturday"``, ``"Sunday"` `};``        ` `        ``// Initialize all counts as 4.``        ``int``[] count = ``new` `int``[``7``];``        ``for` `(``int` `i = ``0``; i < ``7``; i++)``            ``count[i] = ``4``;``        ` `        ``// find index of the first day``        ``int` `pos = ``0``;``        ``for` `(``int` `i = ``0``; i < ``7``; i++)``        ``{``            ``if` `(firstday == days[i])``            ``{``                ``pos = i;``                ``break``;``            ``}``        ``}``        ` `        ``// number of days whose occurrence``        ``// will be 5``        ``int` `inc = n - ``28``;``        ` `        ``// mark the occurrence to be 5 of n-28 days``        ``for` `(``int` `i = pos; i < pos + inc; i++)``        ``{``            ``if` `(i > ``6``)``                ``count[i % ``7``] = ``5``;``            ``else``                ``count[i] = ``5``;``        ``}``        ` `        ``// print the days``        ``for` `(``int` `i = ``0``; i < ``7``; i++)``        ``{``            ``System.out.println(days[i] + ``" "` `+ count[i]);``        ``}``    ``}``    ` `    ``// Driver function``    ``public` `static` `void` `main(String argc[]){``        ``int` `n = ``31``;``        ``String firstday = ``"Tuesday"``;``        ``occurrenceDays(n, firstday);``    ``}``}` `// This code is contributed by Sagar Shukla`

## Python3

 `# Python program to count``# occurrence of days in a month``import` `math` `# function to find occurrences``def` `occurrenceDays( n, firstday):` `    ``# stores days in a week``    ``days ``=` `[ ``"Monday"``, ``"Tuesday"``, ``"Wednesday"``,``           ``"Thursday"``,  ``"Friday"``, ``"Saturday"``,``           ``"Sunday"` `]`` ` `    ``# Initialize all counts as 4.``    ``count``=` `[``4` `for` `i ``in` `range``(``0``,``7``)]``    ` `    ``# find index of the first day``    ``pos``=``-``1``    ``for` `i ``in` `range``(``0``,``7``):``        ``if` `(firstday ``=``=` `days[i]):``            ``pos ``=` `i``            ``break``        ` `    ` ` ` `    ``# number of days whose occurrence will be 5``    ``inc ``=` `n ``-` `28`` ` `    ``# mark the occurrence to be 5 of n-28 days``    ``for`  `i ``in` `range``( pos, pos ``+` `inc):``        ``if` `(i > ``6``):``            ``count[i ``%` `7``] ``=` `5``        ``else``:``            ``count[i] ``=` `5``    ` ` ` `    ``# print the days``    ``for` `i ``in` `range``(``0``,``7``):``        ``print` `(days[i] , ``" "` `, count[i])``    `  ` ` `# driver program to test``# the above function``n ``=` `31``firstday ``=` `"Tuesday"``occurrenceDays(n, firstday)` `# This code is contributed by Gitanjali.`

## C#

 `// C# program to count occurrence of``// days in a month``using` `System;` `public` `class` `GfG {` `    ``// function to find occurrences``    ``public` `static` `void` `occurrenceDays(``int` `n,``                              ``string` `firstday)``    ``{``        ` `        ``// stores days in a week``        ``String[] days = ``new` `String[]{ ``"Monday"``,``                        ``"Tuesday"``, ``"Wednesday"``,``                          ``"Thursday"``, ``"Friday"``,``                        ``"Saturday"``, ``"Sunday"` `};``        ` `        ``// Initialize all counts as 4.``        ``int``[] count = ``new` `int``;``        ` `        ``for` `(``int` `i = 0; i < 7; i++)``            ``count[i] = 4;``        ` `        ``// find index of the first day``        ``int` `pos = 0;``        ``for` `(``int` `i = 0; i < 7; i++)``        ``{``            ``if` `(firstday == days[i])``            ``{``                ``pos = i;``                ``break``;``            ``}``        ``}``        ` `        ``// number of days whose occurrence``        ``// will be 5``        ``int` `inc = n - 28;``        ` `        ``// mark the occurrence to be 5 of``        ``// n-28 days``        ``for` `(``int` `i = pos; i < pos + inc; i++)``        ``{``            ``if` `(i > 6)``                ``count[i % 7] = 5;``            ``else``                ``count[i] = 5;``        ``}``        ` `        ``// print the days``        ``for` `(``int` `i = 0; i < 7; i++)``        ``{``            ``Console.WriteLine(days[i] + ``" "``                                    ``+ count[i]);``        ``}``    ``}``    ` `    ``// Driver function``    ``public` `static` `void` `Main()``    ``{``        ``int` `n = 31;``        ``string` `firstday = ``"Tuesday"``;``        ` `        ``occurrenceDays(n, firstday);``    ``}``}` `// This code is contributed by vt_m.`

## Javascript

 ``

Output:

```Monday 4
Tuesday 5
Wednesday 5
Thursday 5
Friday 4
Saturday 4
Sunday 4```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up