I had to do the same thing
Dude i had to do the same thing for a class that i am in. Below is the code i used and it worked perfect!
/*
* Desc: Tell the day of the year
*/
/* Name: Included Files*/
#include <stdio.h>
/* Name: Leap Year Function */
int
leap(int Year)
{
/* Is it divisible by 100 & 400 or by 4 */
if(Year % 100 == 0 && Year % 400 == 0 || Year % 4 == 0 )
return(1);
else
return(0);
}
/* Name: sMonth - Structure to hold each Month's info */
typedef struct
{
int MonthNum;
int Days_N_Month;
}sMonth;
/* Name: Main Function */
int
main(void)
{
int Month; /* Month */
int Day; /* Day */
int Year; /* Year */
sMonth January; /* Declare as a month */
sMonth Febuary; /* Declare as a month */
sMonth March; /* Declare as a month */
sMonth April; /* Declare as a month */
sMonth May; /* Declare as a month */
sMonth June; /* Declare as a month */
sMonth July; /* Declare as a month */
sMonth August; /* Declare as a month */
sMonth September; /* Declare as a month */
sMonth October; /* Declare as a month */
sMonth November; /* Declare as a month */
sMonth December; /* Declare as a month */
/* Set the valuse for each month */
January.Days_N_Month = 31;
January.MonthNum = 1;
Febuary.Days_N_Month = 28;
Febuary.MonthNum = 2;
March.Days_N_Month = 31;
March.MonthNum = 3;
April.Days_N_Month = 30;
April.MonthNum = 4;
May.Days_N_Month = 31;
May.MonthNum = 5;
June.Days_N_Month = 30;
June.MonthNum = 6;
July.Days_N_Month = 31;
July.MonthNum = 7;
August.Days_N_Month = 31;
August.MonthNum = 8;
September.Days_N_Month = 30;
September.MonthNum = 9;
October.Days_N_Month = 31;
October.MonthNum = 10;
November.Days_N_Month = 30;
November.MonthNum = 11;
December.Days_N_Month = 31;
December.MonthNum = 12;
/* Get the Info needed */
printf("What is the Month, Day, and Year? (ex. January 12 1982 = 1 12 1982)\n");
scanf("%d %d %d" , &Month, &Day, &Year);
/* Test the day of the month and see if it is valid (exit the program if it is not) */
if(Day > 31 || Day < 1)
{
printf("There is not a month with %d days in it.\n",Day);
return(0);
}
/* Test for Leap Year */
if(leap(Year))
Febuary.Days_N_Month += 1;
/* See what day of the year it is */
if(Month == January.MonthNum)
printf("That is the %d day of the year.\n",Day);
else if(Month == Febuary.MonthNum)
printf("That is the %d day of the year.\n",January.Days_N_Month + Day);
else if(Month == March.MonthNum)
printf("That is the %d day of the year.\n",January.Days_N_Month + Febuary.Days_N_Month + Day);
else if(Month == April.MonthNum)
printf("That is the %d day of the year.\n",January.Days_N_Month + Febuary.Days_N_Month + March.Days_N_Month + Day);
else if(Month == May.MonthNum)
printf("That is the %d day of the year.\n",January.Days_N_Month + Febuary.Days_N_Month + March.Days_N_Month + April.Days_N_Month + Day);
else if(Month == June.MonthNum)
printf("That is the %d day of the year.\n",January.Days_N_Month + Febuary.Days_N_Month + March.Days_N_Month + April.Days_N_Month + May.Days_N_Month + Day);
else if(Month == July.MonthNum)
printf("That is the %d day of the year.\n",January.Days_N_Month + Febuary.Days_N_Month + March.Days_N_Month + April.Days_N_Month + May.Days_N_Month + June.Days_N_Month + Day);
else if(Month == August.MonthNum)
printf("That is the %d day of the year.\n",January.Days_N_Month + Febuary.Days_N_Month + March.Days_N_Month + April.Days_N_Month + May.Days_N_Month + June.Days_N_Month + July.Days_N_Month + Day);
else if(Month == September.MonthNum)
printf("That is the %d day of the year.\n",January.Days_N_Month + Febuary.Days_N_Month + March.Days_N_Month + April.Days_N_Month + May.Days_N_Month + June.Days_N_Month + July.Days_N_Month + August.Days_N_Month + Day);
else if(Month == October.MonthNum)
printf("That is the %d day of the year.\n",January.Days_N_Month + Febuary.Days_N_Month + March.Days_N_Month + April.Days_N_Month + May.Days_N_Month + June.Days_N_Month + July.Days_N_Month + August.Days_N_Month + September.Days_N_Month + Day);
else if(Month == November.MonthNum)
printf("That is the %d day of the year.\n",January.Days_N_Month + Febuary.Days_N_Month + March.Days_N_Month + April.Days_N_Month + May.Days_N_Month + June.Days_N_Month + July.Days_N_Month + August.Days_N_Month + September.Days_N_Month + October.Days_N_Month + Day);
else if(Month == December.MonthNum)
printf("That is the %d day of the year.\n",January.Days_N_Month + Febuary.Days_N_Month + March.Days_N_Month + April.Days_N_Month + May.Days_N_Month + June.Days_N_Month + July.Days_N_Month + August.Days_N_Month + September.Days_N_Month + October.Days_N_Month + November.Days_N_Month + Day);
else
printf("Month is invalid.\n");
/* Exit Program*/
return(0);
}