Thread: Need Help with C Program that determines day of the week

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    4

    Need Help with C Program that determines day of the week

    Hi, I've been working on this program for quick a while and I can't seem to figure it out. I have sample Test Data, but my program isn't working for all of them.

    The program is supposed to take as input the appointment date in the form (year, month, day) and then compute which weekeday the appointment falls on. Dates for appointments have to be between January 1, 1901 and December 31, 2299

    Program has to:
    1. Read and validate the appointment date and if invalid print an appropriate message and terminate
    2. Determine if the appointment year is a leap year
    3. Calculate the number of leap years from 1901 to the appointment date.
    4. Calculate the number of days from Jan 1 1901 to the appointment date
    5. Determine the weekday corresponding to the appointment date.

    Code:
    #include<stdio.h>
    
    int main()
    {
    
       int year=0;
       int day=0;
       
       int month=0;
       int week_day=0;
       int total_days,difference,leap_yrs;
    
       
     
    
    
    
       printf("Please enter an appointment day in the form year month day (YYYY MM DD):\n");
       scanf("%d %d %d", &year, &month, &day);
      
    
       /* Checks to see if the input is invalid, and if so prints out a "Date out of range" message and terminates the program*/
    
       if (year<1901 || year>2299)
       {
          printf("Date out of range\n");
          return 0;
       }
    
    
    
    
       if(((year<2000 && year%400 ==0) || (year<2000 && (year%100 != 0 && year%4 == 0))))
       {
          difference = ((year-1)%100);
          leap_yrs = ((year%100) / 4); /*no. of leap years between concerned year and reference year*/
       }
       else if ((((year>2000 && year<=2100) && year%400 ==0) || ((year>2000 && year<=2100) && (year%100 != 0 && year%4 == 0))))
       {
          difference = (((year-1)%100)+100);
          leap_yrs = difference / 4; /*no. of leap years between concerned year and reference year*/     
       }
     
       else if ((((year>2100 && year<=2200) && year%400 ==0) || ((year>2100 && year<=2200) && (year%100 != 0 && year%4 == 0))))
      {
          difference = (((year-1)%100)+200);
          leap_yrs = difference / 4; /*no. of leap years between concerned year and reference year*/     
       }
      
       else if ((((year>2200 && year<=2299) && year%400 ==0) || ((year>2200 && year<=2299) && (year%100 != 0 && year%4 == 0))))
      {
          difference = (((year-1)%100)+300);
          leap_yrs = difference / 4; /*no. of leap years between concerned year and reference year*/     
       }
    
     else if(year>2000 && year<=2100)
       {  
          difference = (((year-1)%100)+100);
          leap_yrs = difference / 4;
       }
       else if ( year>2100 && year<=2200)
       {
          difference = (((year-1)%100)+200);
          leap_yrs = difference / 4;
       }
       else if (year>2200 && year<=2299)
       {
          difference = (((year-1)%100)+300);
          leap_yrs = difference / 4;
       }
       else
          difference = ((year-1)%100);
          leap_yrs = ((year%100) / 4); 
        
    
          total_days = ((difference*365) + (leap_yrs)+(day)) ;
       week_day = total_days % 7;
    
      
    
    
    
    
       
       /*converts month number to month name*/
       printf("",month);
       switch(month)
       {
          case 1: printf("January");
    	 break; 
          case 2: printf("February");
    	 break; 
          case 3: printf("March");
    	 break; 
          case 4: printf("April");
    	 break; 
          case 5: printf("May");
    	 break; 
          case 6: printf("June");
    	 break; 
          case 7: printf("July");
    	 break; 
          case 8: printf("August");
    	 break; 
          case 9: printf("September");
    	 break; 
          case 10: printf("October");
    	 break; 
          case 11: printf("November");
    	 break;
          case 12: printf("December");
    	 break;  
          default: printf("Date out of range");
    	 break;
       }
           
       printf(" %d, %d", day, year); 
       printf(" is a %d", week_day);
       switch (week_day)
       {
          case 1: printf("Tuesday");
    	 break;
          case 2: printf("Wednesday");
    	 break;
          case 3: printf("Thursday");
    	 break;
          case 4: printf("Friday");
    	 break;
          case 5: printf("Saturday");
    	 break;
          case 6: printf("Sunday");
    	 break;
          case 7: printf("Monday");
    	 break;
    
      
       }
       printf("\n");
    
       return 0;
    }

    so when I input 1901 1 1
    I get: January 1, 1901 is a Tuesday (which is correct)

    but then if I input 2299 3 15
    I get: March 15, 2299 is a Thursday (which is INCORRECT, its supposed to be a Wednesday)

    If anyone could please help me I would reallly really really appreciate it.

    Thanks in advance

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Your final else is missing braces.

    Some of your printf statements are pointless.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A program that determines odd numbers
    By metros in forum C Programming
    Replies: 2
    Last Post: 04-21-2010, 03:10 PM
  2. Replies: 8
    Last Post: 03-17-2010, 11:18 PM
  3. Help with day of the week program
    By Punkakitty in forum C++ Programming
    Replies: 10
    Last Post: 01-14-2009, 06:55 PM
  4. Replies: 8
    Last Post: 09-04-2008, 09:56 AM
  5. Depressed, not wanting to program this week
    By EvBladeRunnervE in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 03-08-2003, 11:55 AM