C Language Example 3--enter a certain date of the year, to determine the day is the first days of the year?

Source: Internet
Author: User

question: Enter a certain date of the year, Judge this day is the first days of the year.

   /* Procedure analysis: Take March 5 as an example, should first add up the first two months,
   and then add 5 days that the first days of this year, special circumstances, leap years and enter a
   month greater than 3 o'clock to consider an additional day.
* *
#include <stdio.h>
int main () {
	int day, month, year, sum, Leap;
	printf ("\nplease input year,month,day\n");
	scanf ("%d%d%d", &year,&month,&day);
	Switch (month) {   //first calculates the total number of days before month of month 
		1:sum = 0;break;
		Case 2:sum = 31;break;
		Case 3:sum = 59;break;
		Case 4:sum = 90;break;
		Case 5:sum = 120;break;
		Case 6:sum = 151;break;
		Case 7:sum = 181;break;
		Case 8:sum = 212;break;
		Case 9:sum = 243;break;
		Case 10:sum = 273;break;
		Case 11:sum = 304;break;
		Case 12:sum = 334;break;
		default:printf ("Data error!"); break;
	sum = sum + day;    Plus days of the day 
	if (year%400==0| | (year%4==0&&year%100!=0)) {  //Judge is leap year 
		leap = 1;
	} else{
		leap = 0;
	}
	if (leap = = 1&&month > 2) {  //If it is a leap year and the month is greater than 2, total plus 1 days 
		sum++;
	}
	printf ("It is the%dth day.", sum);
}


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.