作业

56 阅读1分钟
#include <stdio.h> 
int main() { 
  int month, year;
  printf("请输入月份:");
  scanf("%d", &month);
  
  switch (month) { 
   case 1: 
   case 3:
   case 5: 
   case 7:
   case 8:
   case 10:
   case 12:
      printf("该月有31天\n");
      break; 
   case 4: 
   case 6: 
   case 9:
   case 11:
      printf("该月有30天\n");
      break;
   case 2: 
      printf("请输入年份:");
      scanf("%d", &year);
// 判断闰年:能被4整除但不能被100整除,或能被400整除 
      if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
      printf("该月有29天(闰年)\n"); 
      }else {
      printf("该月有28天(平年)\n"); 
} 
      break; 
   default:
      printf("输入的月份无效,请输入1-12之间的数字\n"); 
   }
   
   return 0;
}


image.png