Like Tree1Likes
  • 1 Post By Subsonics

C Programs for Calendar (DIsplay Month and Year)

This is a discussion on C Programs for Calendar (DIsplay Month and Year) within the C Programming forums, part of the General Programming Boards category; This is the code I have so far: Code: #include <stdio.h> char *months[] = { " ", "\nJanuary", "\nFebruary", "\nMarch", ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    13

    C Programs for Calendar (DIsplay Month and Year)

    This is the code I have so far:

    Code:
    #include <stdio.h>
    
    char *months[] =
        {
         " ",
         "\nJanuary", 
         "\nFebruary",
         "\nMarch",
         "\nApril",
         "\nMay",
         "\nJune",
         "\nJuly",
         "\nAugust",
          "\nSeptember",
         "\nOctober",
         "\nNovember",
         "\nDecember"
        };
    
    int month_days[] = {0, 31, 28, 31, 30, 31, 30, 31 ,31 ,30, 31, 30, 31};
    
    int first_day_year(int year)
    {
        int first_day;
        int x;
        int y;
        int z;
    
        first_day = (year + x - y + z) %7;
    
        return first_day;
    }
    
    int leapyear(int year)
    {
        if(year%4 == 0 && year%100 != 0 || year%400 == 0)
        {
            month_days[2] = 29;
            return 1;
        } else {
            month_days[2] = 28;
            return 0;
        }
    }
    
    int calendar(int month, int year, int first_day)
    {
        int i;
    
        printf("%s %d\n\n", months[month], year);
        printf("Sun  Mon  Tue  Wed  Thu  Fri  Sat\n");
    
        for(i = 0; i < month; i++)
        {
            first_day = (first_day + month_days[month]) % 7;
        }
    
        for(i = 1; i <= 1 + first_day * 5; i++)
        {
            printf(" ");
        }
    
        for(i = 1; i <= month_days[month]; i++)
        {
            printf("%2d", i);
    
            if((i + first_day)%7 > 0)
                printf("   ");
            else
                printf("\n ");
        }
    }
    
    int main(void)
    {
        int year;
        int month;
        int first_day;
    
        do{
            printf("Enter a month (1 - 12): ");
            scanf("%d", &month);
        } while (month < 1 || month > 12);
    
        do{
            printf("Enter a year (1978 - 3000): ");
            scanf("%d", &year);
        } while (year < 1978 || year > 3000);
    
        first_day = first_day_year(year);
        leapyear(year);
        calendar(month, year, first_day);
        
        printf("\n");
    
        return 0; 
    }
    The program basically asks for a month and year and then displays the calendar for the input.
    I'm able to compile this code and it works fine for March 2012, but apparently, in other months, the day starts a day before it originally should.

    For example, January 1978 starts on a Sunday, but when I run my program, it starts on a Saturday.

    How can I fix this?
    Please help!

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    x,y and z are uninitialized in your first_day_year() function. Don't know what you expect of this but it will not give you a reliable answer.
    Karyumi likes this.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    13
    @Subsonics, yup. Saw it immediately after I posted this. But even though I fixed it, the program gave even weirder outputs.

    Here's my code now:

    Code:
    #include <stdio.h>
    
    char *months[] =
        {
         " ",
         "\nJanuary", 
         "\nFebruary",
         "\nMarch",
         "\nApril",
         "\nMay",
         "\nJune",
         "\nJuly",
         "\nAugust",
          "\nSeptember",
         "\nOctober",
         "\nNovember",
         "\nDecember"
        };
    
    int month_days[] = {0, 31, 28, 31, 30, 31, 30, 31 ,31 ,30, 31, 30, 31};
    
    int first_day_year(int year)
    {
        int first_day;
        int x;
        int y;
        int z;
    
        x = (year - 1.)/4.0;
        y = (year - 1.)/100.;
        z = (year - 1.)/400.;
    
        first_day = (year + x - y + z) %7;
    
        return first_day;
    }
    
    int leapyear(int year)
    {
        if(year%4 == 0 && year%100 != 0 || year%400 == 0)
        {
            month_days[2] = 29;
            return 1;
        } else {
            month_days[2] = 28;
            return 0;
        }
    }
    
    int calendar(int month, int year, int first_day)
    {
        int i;
    
        printf("%s %d\n\n", months[month], year);
        printf("Sun  Mon  Tue  Wed  Thu  Fri  Sat\n");
    
        for(i = 0; i < month; i++)
        {
            first_day = (first_day + month_days[month]) % 7;
        }
    
        for(i = 1; i <= 1 + first_day * 5; i++)
        {
            printf(" ");
        }
    
        for(i = 1; i <= month_days[month]; i++)
        {
            printf("%2d", i);
    
            if((i + first_day)%7 > 0)
                printf("   ");
            else
                printf("\n ");
        }
    }
    
    int main(void)
    {
        int year;
        int month;
        int first_day;
    
        do{
            printf("Enter a month (1 - 12): ");
            scanf("%d", &month);
        } while (month < 1 || month > 12);
    
        do{
            printf("Enter a year (1978 - 3000): ");
            scanf("%d", &year);
        } while (year < 1978 || year > 3000);
    
        first_day = first_day_year(year);
        leapyear(year);
        calendar(month, year, first_day);
        
        printf("\n");
    
        return 0; 
    }

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Hm, I don't know. There are formulas to get from a Julian day number JDN to a Gregorian date, it's not Jan 1 1978 though. Ditto for converting UNIX epoch, which is seconds since Jan 1 1970.
    Last edited by Subsonics; 03-03-2012 at 11:00 PM.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    13
    Hmm.
    The only problem I have now is where the first day of the month starts.
    I really don't see what I can do to fix this.
    Help, anyone? D:

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help 1-month calendar almost working
    By ImaCnoob in forum C Programming
    Replies: 4
    Last Post: 02-12-2012, 02:28 PM
  2. One month calendar
    By danieldcc in forum C Programming
    Replies: 11
    Last Post: 07-13-2011, 03:18 PM
  3. Display day, month, year in different format
    By melodious in forum C++ Programming
    Replies: 5
    Last Post: 07-05-2007, 01:14 AM
  4. Year, month and day
    By Newbie999 in forum C Programming
    Replies: 15
    Last Post: 12-01-2006, 09:17 AM
  5. getting the next day's day, month, year etc
    By underthesun in forum C Programming
    Replies: 3
    Last Post: 02-17-2005, 06:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21