Get number of days in month

Friday, July 2, 2010

 // C08_Ex 8: Input month,year
// Output dates of this month

#include <stdio.h>

void main()
{
    int month, yr, result;
  
    printf("Input month,year: ");
    scanf("%d%d", &month, &yr);
  
    if (month>=1 && month<=12)
    {
        switch (month)
        {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12: result = 31; break;
        case 4:
        case 6:
        case 9:
        case 11: result = 30; break;
        case 2:
            if (yr % 400 == 0 || (yr % 4 == 0 && yr % 100 != 0))    //  leap year
                result = 29;
            else
                result = 28;
        }

        printf("=> month %d/%d has %d days\n", month, yr, result);
    }
    else
        printf("=> Invalid month!\n");
}

0 Comments:

Post a Comment

 
 
 
 
Copyright © 2010 C Source Code - C++ Source Code. All Rights Reserved. Using Xclear Theme | Bloggerized by Themescook Developed by Helios | Powered by Blogger