Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5946525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:50:08+00:00 2026-05-22T16:50:08+00:00

I’m trying to write a date calculator in C program with the range from

  • 0

I’m trying to write a date calculator in C program with the range from 1/1/1902 to 12/31/2299, I’ve follow the algorithm from http://en.wikipedia.org/wiki/Calculating_the_day_of_the_week with the century table,month table and day table, but when I tried to print out, this is what I have

Enter Year, Month and Day as YYYY,M,DD
1982 4 24
Saturday1982 ,6, 24, is  6

Instead of saying 1982 4 24 is a Saturday

What is wrong in the program I wrote? The placement of the switch cases?

#include <stdio.h>

int main (void)
{
    // insert code here...
    int day,month,year;

    printf("Enter Year, Month and Day as YYYY,M,DD\n");

    scanf("%4d%d%2d", &year, &month, &day);

    if (year >= 1901 && year <= 2299 &&
        month >= 1 && month <= 12 &&
        day >= 0 && day <= 31)
    {
        int century = year/100;

        /* making a century table for calculation*/

        switch(century)
        {
            case 19:
                century=0;
                break;
            case 20:
                century=6;
                break;
            case 21:
                century=4;
                break;
            case 22:
                century=2;
                break;
        }

        int last2_of_year= year % 100;

        /* Last 2 digits of the year entered*/

        int last2_div_4 =  last2_of_year/4;

        switch (month)
        {
            case 1:
                month=0;
                break;

            case 2:
                month=3;
                break;
            case 3:
                month=3;
                break;
            case 4:
                month=6;
                break;
            case 5:
                month=1;
                break;
            case 6:
                month=4;
                break;
            case 7:
                month=6;
                break;
            case 8:
                month=2;
                break;
            case 9:
                month=5;
                break;
            case 10:
                month=0;
                break;
            case 11:
                month=3;
                break;
            case 12:
                month=5;
                break;
         }


        int total_num = (century+ last2_of_year +day +month +last2_div_4)%7;

        switch (total_num) 
        {
            case 0:
            printf("Sunday");
                break;
            case 1:
            printf("Monday");
                break;
            case 2:
            printf("Tuesday");
                break;
            case 3:
            printf("Wednesday");
                break;
            case 4:
            printf("Thursday");
                break;
            case 5:
            printf("Friday");
                break;
            case 6:
            printf("Saturday");
                break;
        }

        printf("%d ,%d, %d, is a %d", year,month,day,total_num);
        }
        else
        {
            printf("invalid\n");
        }



    return 0;

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:50:08+00:00Added an answer on May 22, 2026 at 4:50 pm

    Looks like @QuantumMechanic found the root cause of the problem, but I’d like to suggest a few changes:

        int century = year/100;
    
        /* making a century table for calculation*/
    
        switch(century)
        {
            case 19:
                century=0;
                break;
    

    I’m very leery of using a single variable to represent two different things. Here, century represents both the user-input human-readable century and an offset to the first day of the week for the century. Two variables would give clearer code, and allow you to re-use the century information later, should the need arise.

    Second, using a case statement to store offsets for the months feels a little … overdone:

        switch (month)
        {
            case 1:
                month=0;
                break;
    
            case 2:
                month=3;
                break;
            case 3:
                month=3;
                break;
    

    This could instead be handled with an array lookup:

    int leap_month[] = [-1, 6, 2, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5];
    int norm_month[] = [-1, 0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5];
    
    if (leap_year)
        month_offset = leap_month[month];
    else
        month_offset = norm_month[month];
    

    The -1 is just to allow referring to the table with human-friendly indexes (Jan == 1). Feel free to remove it and use leap_month[month-1] or similar if you find that easier.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.