Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Mr. Sam was born on XX-YY-ZZZZ which happened to be a Saturday. If he died on AA-BB-CCCC, how many birthdays would he celebrate that fall on a Monday?
Assume, the year which is divisible by 4 are leap years and February has 29 days in a leap year. Also, rest of the calendar months have same days as in original calendar.

What I have tried:

I know how I had to convert this in code but don't know the approach of question.
Posted
Updated 6-Jun-22 8:15am
v2
Comments
PIEBALDconsult 4-Jun-22 18:49pm    
"the year which is divisible by 4 are leap years"
No, not if divisible by 400.
Bad spec, send it back.
pppp333000 4-Jun-22 18:51pm    
actually we are assuming it if divisible by 4 then leap year
PIEBALDconsult 4-Jun-22 20:00pm    
But not all of them are.
Greg Utas 5-Jun-22 7:41am    
If it's divisible by 100, it's not a leap year unless it's divisible by 400, in which case it is. 2000 was a leap year and will likely be encountered when running your program.
pppp333000 4-Jun-22 18:52pm    
Can you please help me with this?
I have written code but not able to find logic

I'd probably start by writing a function that was passed a date and returned the day of the week if it was valid.

Then it becomes pretty simple: for each complete year that he lived, call the function and check if it was a Monday.

That's not too bad - and the actual day of the week function isn't too complicated: Determination of the day of the week - Wikipedia[^] will help you there.
The complications come when you realize that we have had two calendars: Julian and Gregorian; and that if he was born on Feb 29th, he gets less birthdays than the rest of us ...

Give it a try: this isn't really a complex task, it just needs you to think about it rather than dive straight into code!
 
Share this answer
 
Comments
merano99 6-Jun-22 13:20pm    
it works, +5
OriginalGriff 6-Jun-22 13:43pm    
Excellent!
Quote:
I know how I had to convert this in code but don't know the approach of question.

Here is how day of week calculation is done.
Determination of the day of the week - Wikipedia[^]
As for any problem, the solving imply to have some knowledge/understanding of the problem.
 
Share this answer
 
You need to start with some Lern C tutorial to get the skills to write the code.
Than make a concept of your program. You task is to check whether some day (here that birthday) is a monday. So you to get a formula how long a year is and on what weekday this day is coming.

tip: think about using the modulo operator to calcukate the change of weekday.
 
Share this answer
 
Quote:
I know how I had to convert this in code but don't know the approach of question.

I understand the approach of the question in such a way that you should write a program that calculates the number of days after entering the data. What else could it mean? And because of the limited calculation, you would also have to limit the input of the year if the result is to match reality.

Based on OriginalGriff's suggestion, I briefly tested it on two examples.
C
date_t b_sam = { 22, 2, 1964 };  // Mr.Sam was born on XX-YY-ZZZZ which happened to be a Saturday.
date_t d_sam = { 1, 8, 2032 };  // died on Sunday

unsigned myears = calcfullyears(b_sam, d_sam); // calculate full years
unsigned bdaycnt = 0;                          // count birthdays 

for (unsigned y = b_sam.y; y <= b_sam.y + myears; y++) {
  // check birthday in leap year
  if ((b_sam.m == 2) && (b_sam.d == 29) && (checkleap(y) == 0)) {
    // does not exist!
  }
  else {
    mydow1 = dayofweek(y, b_sam.m, b_sam.d);
    if (mydow1 == DMON) {
      printf("Year %04u  (on %s)\n", y, DAYNAM[mydow1]);
      bdaycnt++;
    }
  }
}

with:
C
typedef struct {
	unsigned d, m, y;
} date_t;

Birth: 1964 - 2 - 22 (on SAT)
Death : 2064 - 8 - 1 (on FRI)
Year 1965 (on MON)
Year 1971 (on MON)
Year 1982 (on MON)
Year 1988 (on MON)
Year 1993 (on MON)
Year 1999 (on MON)
Year 2010 (on MON)
Year 2016 (on MON)
Year 2021 (on MON)
Year 2027 (on MON)
Year 2038 (on MON)
Year 2044 (on MON)
Year 2049 (on MON)
Year 2055 (on MON)
Mr Sam had 14 birthdays on mondays, he lived 100 full years.

In fact, it is very bitter when the birthday falls on February 29th.

Birth: 1964 - 2 - 29 (on SAT)
Death : 2064 - 8 - 1 (on FRI)
Year 1988 (on MON)
Year 2016 (on MON)
Year 2044 (on MON)
Mr Sam had 3 birthdays on mondays, he lived 100 full years.

If you now knew how many full years Sam lived, you could roughly state it.

Between 1904 and 2096 there seems to be no exceptions in leap years, before and after that there is. In case of February 29th. there would be a lot less to count:
1904, ... ,1960, 1964, 1968, 1972, 1976, 1980, 1984, 1988, 1992, 1996, 2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028, 2032, 2036, ... 2096.

"Assume, the year which is divisible by 4 are leap years."
This assumption is correct at least for the leap years given above. Since the assumption is not always correct, incorrect days compared to the official calculation would be the result. The year range may have to be restricted.
 
Share this answer
 
v7

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900