1

How do I tweak the output of this code:

  • Let's say when I type in 2012. The output should be February 29, 2012 Wednesday is a leap year (same thing with the other leap year)

  • If I type in 2013, the output should be 2013 is not a leap year.

Here's my current code:

include <stdio.h>
int main()
{
    int year;
    printf("Enter a year to check if it is a leap year\n");
    scanf("%d", &year);
    if ( year%400 == 0)
        printf("%d is a leap year.\n", year);
    else if ( year%100 == 0)
        printf("%d is not a leap year.\n", year);
    else if ( year%4 == 0 )
        printf("%d is a leap year.\n", year);
    else
        printf("%d is not a leap year.\n", year);
    return 0;
}

What should I to determine the day of week of February 29th in a leap year?

Asis
  • 683
  • 3
  • 23

2 Answers2

0

Your question makes me Feel like obtaining it myself And start doing research and Codes on it. I've found a piece of code that would return a day by inputting a date pattern . In your case if its would be a leap year the date would be 29,2,year and if its not then 28,2,year.Here 0 represent sunday , 1 represent monday and similarly 6 represent the saturday.

/* A program to find day of a given date */
#include<stdio.h>

int dayofweek(int d, int m, int y)
{
    static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
    y -= m < 3;
    return ( y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}

/* Driver function to test above function */
int main()
{
    int day = dayofweek(10, 10, 2014);
    printf ("%d", day);

    return 0;
}

If you find any error in code or Difficulty to make changes on it. Please let me know I would try to help. Adding this code in your's you would possibly gets your needs.

Asis
  • 683
  • 3
  • 23
-1

As suggested in https://stackoverflow.com/a/21235587/3578315
you can use one liner code
weekday = (d+=m<3?y--:y-2,23*m/9+d+4+y/4-y/100+y/400)%7;
where m=2, d=29 and y=year for your case.
Where weekday starts from Sunday. (0 - Sunday, 1 - Monday,...)

Community
  • 1
  • 1
Rudrik
  • 24
  • 2