fork download
  1. #include <stdio.h>
  2.  
  3.  
  4. int Leap(int year) {
  5. if (year % 400 == 0) return 1;
  6. else if (year % 100 == 0) return 0;
  7. else if (year % 4 == 0) return 1;
  8. else return 0;
  9. }
  10.  
  11.  
  12. int DayMonth(int year, int month) {
  13. if (month == 4 || month == 6 || month == 9 || month == 11)
  14. return 30;
  15. else if (month == 2)
  16. return Leap(year) ? 29 : 28;
  17. else
  18. return 31;
  19. }
  20.  
  21. int Zeller(int year, int month, int day) {
  22. if (month == 1 || month == 2) {
  23. month += 12;
  24. year--;
  25. }
  26. int C = year / 100;
  27. int Y = year % 100;
  28.  
  29. int weekday = ((day + (26 * (month + 1) / 10) + Y + Y / 4 + 5 * C + C / 4) + 5) % 7;
  30. return weekday; // 月曜=0, ..., 日曜=6
  31. }
  32.  
  33. int main() {
  34. int year, month;
  35. printf("西暦年と月を入力してください(例: 2024 6): ");
  36. scanf("%d %d", &year, &month);
  37.  
  38. if (month < 1 || month > 12) {
  39. printf("エラー: 月は1〜12の範囲で入力してください。\n");
  40. return 1;
  41. }
  42.  
  43. int days = DayMonth(year, month);
  44. int weekday = Zeller(year, month, 1); // その月の1日の曜日
  45.  
  46. printf("\n 月 火 水 木 金 土 日\n");
  47.  
  48.  
  49. int i;
  50. for (i = 0; i < weekday; i++) {
  51. printf(" ");
  52. }
  53.  
  54.  
  55. for (i = 1; i <= days; i++) {
  56. printf("%3d", i);
  57. if (weekday == 6)
  58. printf("\n");
  59. weekday++;
  60. weekday %= 7; // 0〜6 に戻す
  61. }
  62.  
  63. if (weekday != 0) printf("\n");
  64.  
  65. return 0;
  66. }
Success #stdin #stdout 0.01s 5308KB
stdin
2025 6
stdout
西暦年と月を入力してください(例: 2024 6): 
  月  火  水  木  金  土  日
                    1
  2  3  4  5  6  7  8
  9 10 11 12 13 14 15
 16 17 18 19 20 21 22
 23 24 25 26 27 28 29
 30