(C syntax) print the number of days in a month, syntax 17

Source: Internet
Author: User

(C syntax) print the number of days in a month, syntax 17

Knowledge point:

Flexible use of logical expressions

Method 1:

Switch () usage, pay attention to the use of case 'A ':......; Break; case 6 :......; Break;

Do not forget break;

Method 2:

If ...... Else usage

Check whether it is a leap year algorithm.

 

Content: print the number of days in a month.

Input description:

Two integers in one row, followed by the year and followed by the month

Output description:

An integer

 

Input example:

2009 6

Output example:

30

 

Method 1:

#include <stdio.h>
int main()
{
 int y,m;
 scanf("%d%d",&y,&m);
 if((y%4==0&&y%100!=0)||y%400==0)
 {
  switch (m)
  {
  case 1: printf("31\n");break;
  case 2: printf("29\n");break;
  case 3: printf("31\n");break;
  case 4: printf("30\n");break;
  case 5: printf("31\n");break;
  case 6: printf("30\n");break;
  case 7: printf("31\n");break;
  case 8: printf("31\n");break;
  case 9: printf("30\n");break;
  case 10: printf("31\n");break;
  case 11: printf("30\n");break;
  case 12: printf("31\n");break;
  }
 }
 else
  switch (m)
 {
  case 1: printf("31\n");break;
  case 2: printf("28\n");break;
  case 3: printf("31\n");break;
  case 4: printf("30\n");break;
  case 5: printf("31\n");break;
  case 6: printf("30\n");break;
  case 7: printf("31\n");break;
  case 8: printf("31\n");break;
  case 9: printf("30\n");break;
  case 10: printf("31\n");break;
  case 11: printf("30\n");break;
  case 12: printf("31\n");break;
 }
 return 0;
}

 

Method 2:

#include <stdio.h>
int main()
{
 int y,m;
 scanf("%d%d",&y,&m);
 if (m==1||m==3||m==5||m==7||m==8||m==10||m==12)
 {
  printf("31\n");
 }
 else 
 {
  if (m==2)
  {
   if((y%4==0&&y%100!=0)||y%400==0)
   {
    printf("29\n");
   }
   else
   {
    printf("28\n");
   }
  }
  else 
  {
   printf("30\n");
  }
 }
 return 0;
}

 


How many days is a program written in java language? It must be a java language and not too complex syntax. I just learned

Calendar c = Calendar. getInstance ();
C. set (Calendar. YEAR, year );
C. set (Calendar. MONTH, month-1); // This value must be reduced by 1, that is, January is 0.
C. set (Calendar. DAY_OF_MOUTH, day );

Int result = c. get (Calendar. DAY_OF_YEAR );
 
I have a problem with the C language. This is caused by many Syntax problems.

1. Pay attention to scanf statements.
2 getDate (y, days, & m, & d );
3 pay attention to the calculation of the number of days swith ()
4 getDate () function
# Include <stdio. h>
Int isLeap (int year );
Void getDate (int y, int days, int * pm, int * pd );
Int getDays (int y, int m, int d );
Int main (void)
{
Int y, m, d, days;
Printf ("Enter _ year_month_day: \ n ");
Scanf ("% d", & y, & m, & d); // quotes of the scanf statement
Days = getDays (y, m, d); // Add d to the number of days
Printf ("% d day of % d: \ n", y, days );

GetDate (y, days, & m, & d );
Printf ("Enter % d: \ n", y, m, d );
Return 0;
}
// Write a subfunction to calculate the day of the year.
Void getDate (int y, int days, int * pm, int * pd)/* y indicates the Year, and days indicates the day of the year, pm points to the monthly value variable of the day in the main function, and * // * pd points to the Daily Value variable of the day in the main function */
{
Int m, d, I;
Int day;
For (I = 1; days> getDays (y, I, 0); I ++)
{
Days = days-getDays (y, I, 0 );
}
M = I;/* m: the monthly value calculated after one day */
D = days;/* d: The daily value calculated after one day */
* Pm = m;
* Pd = d;
}
// Write a sub-function to find that the day of a month is the day of the year.
Int getDays (int y, int m, int d)
{
Int n = 0;
Switch (m)
{
Case 12:
N + = 30;
Case 11:
N + = 31;
Case 10:
N + = 30;
Case 9:
N + = 31;
Case 8:
N + = 31;
Case 7:
N + = 30;
Case 6:
N + = 31;
Case 5:
N + = 30;
Case 4:
N + = 31;
Case 3:
If (isLeap (y) = 1)
N + = 29;
Else
N + = 28;
Case 2:
N + = 31;
Case 1:
N + = d;
Break;
Default:
N + = d;
}
Return n;
}
// Write a subfunction to determine the leap year.
Int isLeap (int year)
{
If (year % 4 = 0) & (year % 100! = 0) | (year % 400 = 0 ))
Return 1;
Else
Return 0; ...... the remaining full text>

Related Article

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.