Open In App

Write a Program to Print 2024 Calendar

Last Updated : 02 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Write a Program to print the 2024 Calendar.

Example:

2024 Calendar:
January 2024
Mo Tu We Th Fr Sa Su
 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             
February 2024
Mo Tu We Th Fr Sa Su
          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          
March 2024
Mo Tu We Th Fr Sa Su
             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 
April 2024
Mo Tu We Th Fr Sa Su
 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                
May 2024
Mo Tu We Th Fr Sa Su
       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       
June 2024
Mo Tu We Th Fr Sa Su
                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 
July 2024
Mo Tu We Th Fr Sa Su
 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             
August 2024
Mo Tu We Th Fr Sa Su
          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    
September 2024
Mo Tu We Th Fr Sa Su
                   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                   
October 2024
Mo Tu We Th Fr Sa Su
    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          
November 2024
Mo Tu We Th Fr Sa Su
             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    
December 2024
Mo Tu We Th Fr Sa Su
                   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                

Method 1: Printing 2024 Calendar Without Using Libraries

Generic Steps to print 2024 Calendar:

  1. Include Minimal Headers (if applicable):
    • Include the essential headers for input/output (Note: In some languages like JavaScript, this step may not be needed).
  2. Define Function to Print a Month:
    • Create a function to print the calendar for a specific month.
  3. Implement the print_month Function:
    • Inside the function, use basic logic to print the calendar for the specified month without relying on external libraries.
  4. Define Function to Print the Entire Calendar:
    • Create another function to iterate over all months and call the print_month function.
  5. Implement the print_calendar Function:
    • Inside the function, iterate over all months (1 to 12) and call the print_month function.
  6. Main Function:
    • Set the desired year and call the print_calendar function in the main function.

Code to print 2024 Calendar:

C++




#include <iostream>
 
void print_month(int year, int month) {
    const char* month_names[] = {"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
 
    std::cout << "\n" << month_names[month] << " " << year << "\n";
    std::cout << "Mo Tu We Th Fr Sa Su\n";
 
    int weekday = (year * 365 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + month * 306 + 5) % 7;
    int days_in_month = 0;
 
    switch (month) {
        case 4: case 6: case 9: case 11:
            days_in_month = 30;
            break;
        case 2:
            days_in_month = ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) ? 29 : 28;
            break;
        default:
            days_in_month = 31;
    }
 
    for (int i = 0; i < (weekday + days_in_month); i++) {
        if (i < weekday)
            std::cout << "   ";
        else
            std::cout << (i - weekday + 1) << ((i + 1) % 7 ? " " : "\n");
    }
}
 
void print_calendar(int year) {
    for (int month = 1; month <= 12; month++) {
        print_month(year, month);
    }
}
 
int main() {
    int year = 2024;
    print_calendar(year);
    return 0;
}


C




#include <stdio.h>
 
void print_month(int year, int month) {
    printf("\n%s %d\n", (month == 1) ? "January" : (month == 2) ? "February" : (month == 3) ? "March" :
                        (month == 4) ? "April" : (month == 5) ? "May" : (month == 6) ? "June" :
                        (month == 7) ? "July" : (month == 8) ? "August" : (month == 9) ? "September" :
                        (month == 10) ? "October" : (month == 11) ? "November" : "December", year);
    printf("Mo Tu We Th Fr Sa Su\n");
 
    int weekday = (year * 365 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + month * 306 + 5) % 7;
    int days_in_month = 0;
 
    switch (month) {
        case 4: case 6: case 9: case 11:
            days_in_month = 30;
            break;
        case 2:
            days_in_month = ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) ? 29 : 28;
            break;
        default:
            days_in_month = 31;
    }
 
    for (int i = 0; i < (weekday + days_in_month); i++) {
        if (i < weekday)
            printf("   ");
        else
            printf("%2d%s", i - weekday + 1, (i + 1) % 7 ? " " : "\n");
    }
}
 
void print_calendar(int year) {
    for (int month = 1; month <= 12; month++) {
        print_month(year, month);
    }
}
 
int main() {
    int year = 2024;
    print_calendar(year);
    return 0;
}


Java




public class CalendarPrint {
 
    public static void printMonth(int year, int month) {
        System.out.println("\n" + getMonthName(month) + " " + year);
        System.out.println("Mo Tu We Th Fr Sa Su");
 
        int weekday = calculateWeekday(year, month);
        int daysInMonth = getDaysInMonth(year, month);
 
        for (int i = 1; i <= daysInMonth + weekday - 1; i++) {
            if (i < weekday)
                System.out.print("   ");
            else
                System.out.printf("%2d%s", i - weekday + 1, (i % 7 == 0) ? "\n" : " ");
        }
    }
 
    public static void printCalendar(int year) {
        for (int month = 1; month <= 12; month++) {
            printMonth(year, month);
        }
    }
 
    public static void main(String[] args) {
        int year = 2024;
        printCalendar(year);
    }
 
    private static String getMonthName(int month) {
        String[] monthNames = {"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
        return monthNames[month];
    }
 
    private static int calculateWeekday(int year, int month) {
        return (year * 365 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + month * 306 + 5) % 7;
    }
 
    private static int getDaysInMonth(int year, int month) {
        switch (month) {
            case 4: case 6: case 9: case 11:
                return 30;
            case 2:
                return ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) ? 29 : 28;
            default:
                return 31;
        }
    }
}


Python3




def print_month(year, month):
    month_names = ["", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
 
    print(f"\n{month_names[month]} {year}")
    print("Mo Tu We Th Fr Sa Su")
 
    weekday = (year * 365 + (year - 1) // 4 - (year - 1) // 100 + (year - 1) // 400 + month * 306 + 5) % 7
    days_in_month = 0
 
    if month in [4, 6, 9, 11]:
        days_in_month = 30
    elif month == 2:
        days_in_month = 29 if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0) else 28
    else:
        days_in_month = 31
 
    for i in range(weekday + days_in_month):
        if i < weekday:
            print("   ", end="")
        else:
            print(f"{(i - weekday + 1):2d}", end=" " if (i + 1) % 7 else "\n")
 
 
def print_calendar(year):
    for month in range(1, 13):
        print_month(year, month)
 
 
if __name__ == "__main__":
    current_year = 2024
    print_calendar(current_year)


Javascript




function printMonth(year, month) {
    console.log(`\n${getMonthName(month)} ${year}`);
    console.log("Mo Tu We Th Fr Sa Su");
 
    const weekday = calculateWeekday(year, month);
    const daysInMonth = getDaysInMonth(year, month);
 
    for (let i = 1; i <= daysInMonth + weekday - 1; i++) {
        if (i < weekday)
            process.stdout.write("   ");
        else
            process.stdout.write(`${(i - weekday + 1).toString().padStart(2, ' ')} ${((i + 1) % 7 === 0) ? '\n' : ' '}`);
    }
}
 
function printCalendar(year) {
    for (let month = 1; month <= 12; month++) {
        printMonth(year, month);
    }
}
 
const currentYear = 2024;
printCalendar(currentYear);
 
function getMonthName(month) {
    const monthNames = ["", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
    return monthNames[month];
}
 
function calculateWeekday(year, month) {
    return (year * 365 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + month * 306 + 5) % 7;
}
 
function getDaysInMonth(year, month) {
    switch (month) {
        case 4: case 6: case 9: case 11:
            return 30;
        case 2:
            return ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) ? 29 : 28;
        default:
            return 31;
    }
}


Output (2024 Calendar):


January 2024
Mo Tu We Th Fr Sa Su
 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             
February 2024
Mo Tu We Th Fr Sa Su
          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          
March 2024
Mo Tu We Th Fr Sa Su
             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 
April 2024
Mo Tu We Th Fr Sa Su
 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                
May 2024
Mo Tu We Th Fr Sa Su
       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       
June 2024
Mo Tu We Th Fr Sa Su
                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 
July 2024
Mo Tu We Th Fr Sa Su
 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             
August 2024
Mo Tu We Th Fr Sa Su
          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    
September 2024
Mo Tu We Th Fr Sa Su
                   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                   
October 2024
Mo Tu We Th Fr Sa Su
    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          
November 2024
Mo Tu We Th Fr Sa Su
             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    
December 2024
Mo Tu We Th Fr Sa Su
                   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                

Method 2: Printing 2024 Calendar Using Libraries

Generic Steps to print 2024 Calendar:

  1. Include Necessary Headers:
    • Include the required headers for input/output, formatting, and time manipulation.
  2. Define Function to Print a Month:
    • Create a function to print the calendar for a specific month.
  3. Implement the print_month Function:
    • Inside the function, use library functions to print the calendar for the specified month.
  4. Define Function to Print the Entire Calendar:
    • Create another function to iterate over all months and call the print_month function.
  5. Implement the print_calendar Function:
    • Inside the function, iterate over all months (1 to 12) and call the print_month function.
  6. Main Function:
    • Set the desired year and call the print_calendar function in the main function.

Code to print 2024 Calendar:

C++




#include <iostream>
#include <iomanip>
#include <ctime>
 
using namespace std;
 
void print_month(int year, int month) {
    tm timeinfo = {0};
    timeinfo.tm_year = year - 1900;
    timeinfo.tm_mon = month - 1;
    timeinfo.tm_mday = 1;
    mktime(&timeinfo);
 
    time_t time = mktime(&timeinfo);
    cout << "\n" << put_time(gmtime(&time), "%B %Y\n");
    cout << "Mo Tu We Th Fr Sa Su\n";
 
    int days_in_month = 0;
    switch (month) {
        case 4: case 6: case 9: case 11:
            days_in_month = 30;
            break;
        case 2:
            days_in_month = ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) ? 29 : 28;
            break;
        default:
            days_in_month = 31;
    }
 
    int weekday = timeinfo.tm_wday;
 
    for (int i = 0; i < (weekday + days_in_month); i++) {
        if (i < weekday)
            cout << "   ";
        else
            cout << setw(2) << i - weekday + 1 << ((i + 1) % 7 ? " " : "\n");
    }
}
 
void print_calendar(int year) {
    for (int month = 1; month <= 12; month++) {
        print_month(year, month);
    }
}
 
int main() {
    int year = 2024;
    print_calendar(year);
    return 0;
}


C




#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
void print_month(int year, int month) {
    char* month_names[] = {"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
 
    printf("\n%s %d\n", month_names[month], year);
    printf("Mo Tu We Th Fr Sa Su\n");
 
    struct tm timeinfo = { .tm_year = year - 1900, .tm_mon = month - 1, .tm_mday = 1 };
    mktime(&timeinfo);
 
    int weekday = timeinfo.tm_wday;
    int days_in_month = 0;
 
    switch (month) {
        case 4: case 6: case 9: case 11:
            days_in_month = 30;
            break;
        case 2:
            days_in_month = ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) ? 29 : 28;
            break;
        default:
            days_in_month = 31;
    }
 
    for (int i = 0; i < (weekday + days_in_month); i++) {
        if (i < weekday)
            printf("   ");
        else
            printf("%2d%s", i - weekday + 1, (i + 1) % 7 ? " " : "\n");
    }
}
 
void print_calendar(int year) {
    for (int month = 1; month <= 12; month++) {
        print_month(year, month);
    }
}
 
int main() {
    int year = 2024;
    print_calendar(year);
    return 0;
}


Java




import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
public class CalendarPrint {
 
    public static void printMonth(int year, int month) {
        SimpleDateFormat sdf = new SimpleDateFormat("MMMM yyyy");
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, 1);
 
        System.out.println("\n" + sdf.format(calendar.getTime()));
        System.out.println("Mo Tu We Th Fr Sa Su");
 
        int weekday = calendar.get(Calendar.DAY_OF_WEEK);
        int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
 
        for (int i = 1; i <= daysInMonth + weekday - 1; i++) {
            if (i < weekday)
                System.out.print("   ");
            else
                System.out.printf("%2d%s", i - weekday + 1, (i % 7 == 0) ? "\n" : " ");
        }
    }
 
    public static void printCalendar(int year) {
        for (int month = 1; month <= 12; month++) {
            printMonth(year, month);
        }
    }
 
    public static void main(String[] args) {
        int year = 2024;
        printCalendar(year);
    }
}


Python3




import calendar
 
def print_month(year, month):
    cal = calendar.monthcalendar(year, month)
    month_name = calendar.month_name[month]
 
    print(f"\n{month_name} {year}")
    print("Mo Tu We Th Fr Sa Su")
 
    for week in cal:
        for day in week:
            if day == 0:
                print("   ", end="")
            else:
                print(f"{day:2d} ", end="")
        print()
 
def print_calendar(year):
    for month in range(1, 13):
        print_month(year, month)
 
if __name__ == "__main__":
    year = 2024
    print_calendar(year)


Javascript




const { getMonth, getYear, getDate } = require('date-fns');
 
function printMonth(year, month) {
    const monthNames = ["", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
 
    console.log(`\n${monthNames[month]} ${year}`);
    console.log("Mo Tu We Th Fr Sa Su");
 
    const firstDay = new Date(year, month - 1, 1).getDay();
    const daysInMonth = new Date(year, month, 0).getDate();
 
    for (let i = 0; i < firstDay + daysInMonth; i++) {
        if (i < firstDay)
            process.stdout.write("   ");
        else
            process.stdout.write(`${(i - firstDay + 1).toString().padStart(2, ' ')} ${((i + 1) % 7 === 0) ? '\n' : ' '}`);
    }
}
 
function printCalendar(year) {
    for (let month = 1; month <= 12; month++) {
        printMonth(year, month);
    }
}
 
const currentYear = getYear(new Date());
printCalendar(currentYear);


Output (2024 Calendar):


January 2024
Mo Tu We Th Fr Sa Su
 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             
February 2024
Mo Tu We Th Fr Sa Su
          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          
March 2024
Mo Tu We Th Fr Sa Su
             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 
April 2024
Mo Tu We Th Fr Sa Su
 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                
May 2024
Mo Tu We Th Fr Sa Su
       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       
June 2024
Mo Tu We Th Fr Sa Su
                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 
July 2024
Mo Tu We Th Fr Sa Su
 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             
August 2024
Mo Tu We Th Fr Sa Su
          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    
September 2024
Mo Tu We Th Fr Sa Su
                   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                   
October 2024
Mo Tu We Th Fr Sa Su
    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          
November 2024
Mo Tu We Th Fr Sa Su
             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    
December 2024
Mo Tu We Th Fr Sa Su
                   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                



Article Tags :