Tuesday, July 9, 2019

C Program to calculate the day of year from the date

Write a C Program to calculate the day of year from the date

PROGRAM:

#include<stdio.h>

int main(void)
{
    int day, mon, year, days_in_feb = 28,
            doy;    // day of year

    printf("Enter date (MM/DD/YYYY): ");
    scanf("%d/%d/%d", &mon, &day, &year);
   
    doy = day;
   
    // check for leap year
    if( (year % 4 == 0 && year % 100 != 0 ) || (year % 400 == 0) )
    {
        days_in_feb = 29;
    }

    switch(mon)
    {
        case 2:
            doy += 31;
            break;
        case 3:
            doy += 31+days_in_feb;
            break;
        case 4:
            doy += 31+days_in_feb+31;
            break;
        case 5:
            doy += 31+days_in_feb+31+30;
            break;
        case 6:
            doy += 31+days_in_feb+31+30+31;
            break;
        case 7:
            doy += 31+days_in_feb+31+30+31+30;
            break;           
        case 8:
            doy += 31+days_in_feb+31+30+31+30+31;
            break;
        case 9:
            doy += 31+days_in_feb+31+30+31+30+31+31;
            break;
        case 10:
            doy += 31+days_in_feb+31+30+31+30+31+31+30;           
            break;           
        case 11:
            doy += 31+days_in_feb+31+30+31+30+31+31+30+31;           
            break;                       
        case 12:
            doy += 31+days_in_feb+31+30+31+30+31+31+30+31+30;           
            break;                                   
    }
   
    printf("Day of year: %d", doy);
   
    return 0;
}

OUTPUT:-

Enter date (MM/DD/YYYY): 7/10/2019
Day of year: 191
--------------------------------
Process exited after 32.04 seconds with return value 0
Press any key to continue . . .

0 Comments:

Post a Comment