Like Tree1Likes
  • 1 Post By Subsonics

C Program for Calendar

This is a discussion on C Program for Calendar within the C Programming forums, part of the General Programming Boards category; Hello, I'm new to C and I need to make a program wherein the user inputs a number (1-12 corresponding ...

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

    C Program for Calendar

    Hello, I'm new to C and I need to make a program wherein the user inputs a number (1-12 corresponding to each month) and a year ( from 1978-3000).
    The output should be the calendar for the specified date and year.
    There should be 7 columns, one for each day. I should also take leap years into consideration which happens every 4 years, but not every 100 years, but happens again every 400 years.

    I have this so far.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char month_name[12][9] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    
    int  day_count(int month, int year)
    {
        int day = 0;
    
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
            day = 31;
        } else if (month == 4 || month == 6 || month  == 9 || month == 11){
            day = 30;
        } else if (month == 2){
            if (year%4 == 0 && year%100 != 0 && year%400 == 0){
                day = 29;
            } else {
                day = 28;
            }
        }
        
        return day;
    }
    
    int first_day(int year)
    {
        int init_pos;
        int x;
        int y;
        int z;
    
        x = (year - 1)/4.0;
        y = (year - 1)/100.0;
        z = (year - 1)/400.0;
    
        init_pos = (year + x - y + z) % 7;
    
        return init_pos;
    }
    
    int main(void)
    {
        int month;
        int year;
    
        int i;
    
        do{
            printf("Enter month (1 - 12):     ");
            scanf("%d", &month);
        } while (month < 1 || month > 12);
    
        do{
            printf("Enter year (1978 - 3000):     ");
            scanf("%d", &year);
        } while (year < 1978 || year > 3000);
    
        //printf("%s", month_name[month-1]);
    
        printf("\n       %d Calendar       \n", year);
        printf("%s\n", month_name[month-1]);
        printf("Sun Mon Tue Wed Thu Fri Sat\n");
        
        return 0;
    }
    I can't think of how I could print out the numbers under the name of the days.
    I'm also not sure if my formula for my first_day function will give the right day for each month of the year.

    I really need help.
    Thanks!

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You could try something like:

    Code:
    const char *weekday[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
    
    int i = daynumber % 7;
    printf("%s\n", weekday[i]);
    Karyumi likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to convert from solar calendar to lunar calendar??
    By zaracattle in forum C++ Programming
    Replies: 7
    Last Post: 11-30-2006, 07:17 AM
  2. Yet another Calendar Program.
    By duffmckagan in forum C Programming
    Replies: 9
    Last Post: 07-14-2006, 11:29 PM
  3. Help with calendar program please
    By blindleaf in forum C Programming
    Replies: 1
    Last Post: 10-31-2002, 11:11 AM
  4. calendar program
    By ds123@rediffmail.com in forum C Programming
    Replies: 1
    Last Post: 02-11-2002, 10:44 AM
  5. calendar program
    By dharmastum in forum C Programming
    Replies: 3
    Last Post: 01-25-2002, 05:35 AM

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