fork(1) 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 == 2) {
  14. return Leap(year) ? 29 : 28;
  15. } else if (month == 4 || month == 6 || month == 9 || month == 11) {
  16. return 30;
  17. } else {
  18. return 31;
  19. }
  20. }
  21.  
  22. // 月曜=0
  23. int Zeller(int year, int month, int day) {
  24. if (month == 1 || month == 2) {
  25. month += 12;
  26. year--;
  27. }
  28. int Y = year % 100;
  29. int C = year / 100;
  30. int weekday = ((day + (26 * (month + 1) / 10) + Y + Y / 4 + 5 * C + C / 4) + 5) % 7;
  31. return weekday;
  32. }
  33.  
  34. int main() {
  35. int year, month;
  36. printf("西暦年を入力してください: ");
  37. scanf("%d", &year);
  38. printf("月を入力してください: ");
  39. scanf("%d", &month);
  40.  
  41. int weekday = Zeller(year, month, 1); // その月の1日の曜日番号
  42. int days = DayMonth(year, month); // その月の日数
  43.  
  44. printf("\n月曜 火曜 水曜 木曜 金曜 土曜 日曜\n");
  45.  
  46. // 空欄部分:カレンダー先頭の空白表示
  47. // → 1日の曜日番号分だけ空白を表示(改行しない)
  48. for (int i = 0; i < weekday; i++) { // ← ★空欄①:i < weekday
  49. printf(" ");
  50. }
  51.  
  52. // 日付の表示ループ
  53. for (int i = 1; i <= days; i++) { // ← ★空欄②:i <= days
  54. printf("%4d", i); // ← ★空欄③:%4dにすると綺麗
  55.  
  56. if (weekday == 6) { // ← ★空欄④:weekday == 6(=日曜)
  57. printf("\n"); // 日曜の後は改行
  58. }
  59.  
  60. weekday = (weekday + 1) % 7; // 曜日を1進めて、0~6の範囲に保つ
  61. }
  62.  
  63. printf("\n");
  64. return 0;
  65. }
Success #stdin #stdout 0s 5320KB
stdin
2025
7
stdout
西暦年を入力してください: 月を入力してください: 
月曜 火曜 水曜 木曜 金曜 土曜 日曜
       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  31