C programming example 4

Source: Internet
Author: User

Question: enter a day of a year to determine the day of the year?

1. program Analysis: Taking June 1, March 5 as an example, we should add up the previous two months, and then add 5 days, that is, the day of the current year. In special cases, an additional day is required when the input month is greater than 3 in a leap year.

2. program source code:

# Include <stdio. h>

Int main (void)
{
Int day, month, year, countday, leap;
Printf ("\ nplease input year, month and day \ n ");
Scanf ("% d", & year, & month, & Day );
If (Year % 4 = 0 & amp; Year % 100! = 0) | (Year % 400 = 0 ))
Leap = 1;
Else
Leap = 0;
Switch (month)
{
Case 1:
Countday = 0;
If (day <= 0 | day> 31)
Return 1;
Break;
Case 2:
Countday = 31;
If (leap! = 1)
{
If (day <= 0 | day> 28)
Return 1;
}
Else
{
If (day <= 0 | day> 29)
Return 1;
}
Break;
Case 3:
Countday = 59;
If (day <= 0 | day> 31)
Return 1;
Break;
Case 4:
Countday = 90;
If (day <= 0 | day> 30)
Return 1;
Break;
Case 5:
Count day = 120;
If (day <= 0 | day> 31)
Return 1;
Break;
Case 6:
Count day = 151;
If (day <= 0 | day> 30)
Return 1;
Break;
Case 7:
Count day = 181;
If (day <= 0 | day> 31)
Return 1;
Break;
Case 8:
Count day = 212;
If (day <= 0 | day> 31)
Return 1;
Break;
Case 9:
Count day = 243;
If (day <= 0 | day> 30)
Return 1;
Break;
Case 10:
Count day = 273;
If (day <= 0 | day> 31)
Return 1;
Break;
Case 11:
Count day = 304;
If (day <= 0 | day> 30)
Return 1;
Break;
Case 12:
Count day = 334;
If (day <= 0 | day> 31)
Return 1;
Break;
Default:
Printf ("input month error! \ N ");
Return 1;
Break;
}
Countday = countday + day + 1;
Printf ("this is the % DTH of this years! \ N ", countday );
Return 0;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.