Print February Calendar

A specific year Y and the starting day of the year D is given as input. Print the February month calendar for that specific year as shown in the Example Input/Output section.

Input/Output Format:
Input:

First line contains the year Y and starting day D separated by a space.

Output:
Print the February month calendar for the given year Y.

Example Input/Output 1:
Input:

2018 MON

Output:
S M T W T F S
* * * * 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 * * * 

Example Input/Output 2:
Input:

1597 WED

Output:
S M T W T F S
* * * * * * 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 * 

def main():
    # Read the year and starting day from the user
    year, day = input().split()
    year = int(year)

    # Determine the number of days in February based on leap year
    month = 29 if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0) else 28

    # Mapping of starting day to the corresponding day index (0 - Sunday, 1 - Monday, etc.)
    days = {"MON": 5, "TUE": 6, "WED": 7, "THU": 1, "FRI": 2, "SAT": 3, "SUN": 4}
    s = days[day]

    # 2D array to represent the calendar
    a = [[0 for _ in range(8)] for _ in range(8)]
    t = 1  # Initialize the day counter

    # Populate the calendar with dates
    for i in range(1, 6):
        if i == 1:
            for j in range(s, 8):
                a[i][j] = t
                t += 1
        else:
            for j in range(1, 8):
                if t <= month:
                    a[i][j] = t
                    t += 1

    # Print the calendar header
    print("S M T W T F S")

    # Determine the number of weeks in February (5 or 6)
    v = 6 if a[5][1] != 0 else 5

    # Print the calendar content
    for i in range(1, v):
        for j in range(1, 8):
            if a[i][j] != 0:
                print(a[i][j], end=" ")
            else:
                print("*", end=" ")
        print()


if __name__ == "__main__":
    main()
#include <iostream>
#include <string>
using namespace std;

int main() {
    int year, month, s, t = 0, v;
    string day;

    // Read the year and starting day from the user
    cin >> year >> day;

    // Determine the number of days in February based on leap year
    month = (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)) ? 29 : 28;

    // Mapping of starting day to the corresponding day index (0 - Sunday, 1 - Monday, etc.)
    int days[7] = {0, 5, 6, 7, 1, 2, 3, 4};
    s = days[day == "SUN" ? 0 : (day == "MON" ? 1 : (day == "TUE" ? 2 : (day == "WED" ? 3 : (day == "THU" ? 4 : (day == "FRI" ? 5 : 6)))))];

    // 2D array to represent the calendar
    int a[8][8] = {0};

    // Populate the calendar with dates
    for (int i = 1; i < 6; i++) {
        if (i == 1) {
            for (int j = s; j < 8; j++) {
                a[i][j] = ++t;
            }
        } else {
            for (int j = 1; j < 8; j++) {
                if (t < month) {
                    a[i][j] = ++t;
                }
            }
        }
    }

    // Print the calendar header
    cout << "S M T W T F S" << endl;

    // Determine the number of weeks in February (5 or 6)
    v = (a[5][1] != 0) ? 6 : 5;

    // Print the calendar content
    for (int i = 1; i < v; i++) {
        for (int j = 1; j < 8; j++) {
            if (a[i][j] != 0) {
                cout << a[i][j] << " ";
            } else {
                cout << "* ";
            }
        }
        cout << endl;
    }

    return 0;
}
#include <stdio.h>
#include <string.h>

int main() {
    int year, month, s, t = 0, v;
    char day[4];

    // Read the year and starting day from the user
    scanf("%d %s", &year, day);

    // Determine the number of days in February based on leap year
    month = (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)) ? 29 : 28;

    // Mapping of starting day to the corresponding day index (0 - Sunday, 1 - Monday, etc.)
    int days[7] = {0, 5, 6, 7, 1, 2, 3, 4};
    if (strcmp(day, "SUN") == 0) s = days[0];
    else if (strcmp(day, "MON") == 0) s = days[1];
    else if (strcmp(day, "TUE") == 0) s = days[2];
    else if (strcmp(day, "WED") == 0) s = days[3];
    else if (strcmp(day, "THU") == 0) s = days[4];
    else if (strcmp(day, "FRI") == 0) s = days[5];
    else if (strcmp(day, "SAT") == 0) s = days[6];

    // 2D array to represent the calendar
    int a[8][8] = {0};

    // Populate the calendar with dates
    for (int i = 1; i < 6; i++) {
        if (i == 1) {
            for (int j = s; j < 8; j++) {
                a[i][j] = ++t;
            }
        } else {
            for (int j = 1; j < 8; j++) {
                if (t < month) {
                    a[i][j] = ++t;
                }
            }
        }
    }

    // Print the calendar header
    printf("S M T W T F Sn");

    // Determine the number of weeks in February (5 or 6)
    v = (a[5][1] != 0) ? 6 : 5;

    // Print the calendar content
    for (int i = 1; i < v; i++) {
        for (int j = 1; j < 8; j++) {
            if (a[i][j] != 0) {
                printf("%d ", a[i][j]);
            } else {
                printf
#include <stdio.h>
#include <string.h>

int main() {
    int year, month, s, t = 0, v;
    char day[4];

    // Read the year and starting day from the user
    scanf("%d %s", &year, day);

    // Determine the number of days in February based on leap year
    month = (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)) ? 29 : 28;

    // Mapping of starting day to the corresponding day index (0 - Sunday, 1 - Monday, etc.)
    int days[7] = {0, 5, 6, 7, 1, 2, 3, 4};
    if (strcmp(day, "SUN") == 0) s = days[0];
    else if (strcmp(day, "MON") == 0) s = days[1];
    else if (strcmp(day, "TUE") == 0) s = days[2];
    else if (strcmp(day, "WED") == 0) s = days[3];
    else if (strcmp(day, "THU") == 0) s = days[4];
    else if (strcmp(day, "FRI") == 0) s = days[5];
    else if (strcmp(day, "SAT") == 0) s = days[6];

    // 2D array to represent the calendar
    int a[8][8] = {0};

    // Populate the calendar with dates
    for (int i = 1; i < 6; i++) {
        if (i == 1) {
            for (int j = s; j < 8; j++) {
                a[i][j] = ++t;
            }
        } else {
            for (int j = 1; j < 8; j++) {
                if (t < month) {
                    a[i][j] = ++t;
                }
            }
        }
    }

    // Print the calendar header
    printf("S M T W T F Sn");

    // Determine the number of weeks in February (5 or 6)
    v = (a[5][1] != 0) ? 6 : 5;

    // Print the calendar content
    for (int i = 1; i < v; i++) {
        for (int j = 1; j < 8; j++) {
            if (a[i][j] != 0) {
                printf("%d ", a[i][j]);
            } else {
                printf("* ");
            }
        }
        printf("n");
    }

    return 0;
}
using System;

public class Program {
    public static void Main() {
        int year, month, s, t = 0, v;
        string day;
        string[] days = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };

        // Read the year and starting day from the user
        string[] input = Console.ReadLine().Split();
        year = int.Parse(input[0]);
        day = input[1];

        // Determine the number of days in February based on leap year
        month = (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)) ? 29 : 28;

        // Get the starting day index (0 - Sunday, 1 - Monday, etc.)
        s = Array.IndexOf(days, day);

        // 2D array to represent the calendar
        int[,] a = new int[8, 8];

        // Populate the calendar with dates
        for (int i = 1; i < 6; i++) {
            if (i == 1) {
                for (int j = s; j < 8; j++) {
                    a[i, j] = ++t;
                }
            } else {
                for (int j = 1; j < 8; j++) {
                    if (t < month) {
                        a[i, j] = ++t;
                    }
                }
            }
        }

        // Print the calendar header
        Console.WriteLine("S M T W T F S");

        // Determine the number of weeks in February (5 or 6)
        v = (a[5, 1] != 0) ? 6 : 5;

        // Print the calendar content
        for (int i = 1; i < v; i++) {
            for (int j = 1; j < 8; j++) {
                if (a[i, j] != 0) {
                    Console.Write(a[i, j] + " ");
                } else {
                    Console.Write("* ");
                }
            }
            Console.WriteLine();
        }
    }
}
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int year, month, s, t = 0, v;
        String day;
        String[] days = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };

        // Read the year and starting day from the user
        String[] input = scanner.nextLine().split(" ");
        year = Integer.parseInt(input[0]);
        day = input[1];

        // Determine the number of days in February based on leap year
        month = (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)) ? 29 : 28;

        // Get the starting day index (0 - Sunday, 1 - Monday, etc.)
        s = 0;
        for (int i = 0; i < days.length; i++) {
            if (days[i].equals(day)) {
                s = i;
                break;
            }
        }

        // 2D array to represent the calendar
        int[][] a = new int[8][8];

        // Populate the calendar with dates
        for (int i = 1; i < 6; i++) {
            if (i == 1) {
                for (int j = s; j < 8; j++) {
                    a[i][j] = ++t;
                }
            } else {
                for (int j = 1; j < 8; j++) {
                    if (t < month) {
                        a[i][j] = ++t;
                    }
                }
            }
        }

        // Print the calendar header
        System.out.println("S M T W T F S");

        // Determine the number of weeks in February (5 or 6)
        v = (a[5][1] != 0) ? 6 : 5;

        // Print the calendar content
        for (int i = 1; i < v; i++) {
            for (int j = 1; j < 8; j++) {
                if (a[i][j] != 0) {
                    System.out.print(a[i][j] + " ");
                } else {
                    System.out.print("* ");
                }
            }
            System.out.println();
        }

        scanner.close();
    }
}
const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

function main() {
  let year, month, s, t = 0, v;
  let day;
  const days = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"];

  rl.question("Enter the year and starting day (e.g., 2023 MON): ", input => {
    [year, day] = input.split(" ").map(e => isNaN(e) ? e : parseInt(e));

    // Determine the number of days in February based on leap year
    month = (year % 400 === 0) || ((year % 4 === 0) && (year % 100 !== 0)) ? 29 : 28;

    // Get the starting day index (0 - Sunday, 1 - Monday, etc.)
    s = days.indexOf(day);

    // 2D array to represent the calendar
    const a = Array.from({ length: 8 }, () => Array(8).fill(0));

    // Populate the calendar with dates
    for (let i = 1; i < 6; i++) {
      if (i === 1) {
        for (let j = s; j < 8; j++) {
          a[i][j] = ++t;
        }
      } else {
        for (let j = 1; j < 8; j++) {
          if (t < month) {
            a[i][j] = ++t;
          }
        }
      }
    }

    // Print the calendar header
    console.log("S M T W T F S");

    // Determine the number of weeks in February (5 or 6)
    v = a[5][1] !== 0 ? 6 : 5;

    // Print the calendar content
    for (let i = 1; i < v; i++) {
      for (let j = 1; j < 8; j++) {
        if (a[i][j] !== 0) {
          process.stdout.write(a[i][j] + " ");
        } else {
          process.stdout.write("* ");
        }
      }
      console.log();
    }

    rl.close();
  });
}

main();

Leave a Reply

Your email address will not be published. Required fields are marked *

More posts. You may also be interested in.