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

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 <bits/stdc++.h> ` `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[7]; ` ` ` `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 fucntion ` `int` `main() ` `{ ` ` ` `int` `n = 31; ` ` ` `string firstday = ` `"Tuesday"` `; ` ` ` `occurrenceDays(n, firstday); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 fucntion ` `n ` `=` `31` `firstday ` `=` `"Tuesday"` `occurrenceDays(n, firstday) ` ` ` `# This code is contributed by Gitanjali. ` |

*chevron_right*

*filter_none*

## 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` `[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++) ` ` ` `{ ` ` ` `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. ` |

*chevron_right*

*filter_none*

**Output:**

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

## Recommended Posts:

- Given an array of size n and a number k, find all elements that appear more than n/k times
- XOR of numbers that appeared even number of times in given Range
- Minimum number of times A has to be repeated such that B is a substring of it
- Print all the sum pairs which occur maximum number of times
- Check if the given array can be reduced to zeros with the given operation performed given number of times
- Find row number of a binary matrix having maximum number of 1s
- Calendar Functions in Python | Set 1( calendar(), month(), isleap()...)
- Difference between two given times
- Remove elements from the array which appear more than k times
- Remove elements that appear strictly less than k times
- Largest sub-string where all the characters appear at least K times
- Largest element in the array that is repeated exactly k times
- Find the row with maximum number of 1s
- Find the number of zeroes
- Find the Missing Number

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.