SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

Calendar Printing Program in Java and Python

16 February 2016

Calendar printing program with algorithm, explanation, Java solution and simple Python solution for ICSE and ISC students.

Question:

Write a program to accept the year, month and the weekday name of the first day of that month and generate its calendar.

INPUT: Enter the year: 2016 Enter the month name: February Enter the weekday name of 1st day of February: Monday OUTPUT: ---------------------------------------------------- February 2016 ---------------------------------------------------- SUN MON TUE WED THU FRI SAT ---------------------------------------------------- 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

Algorithm:

Step 1: Start.

Step 2: Accept the year, month name and weekday name of the first day.

Step 3: Store month names and their normal number of days in two arrays.

Step 4: Check whether the year is a leap year; if yes, change February days from 28 to 29.

Step 5: Search the month array to find the number of days in the entered month.

Step 6: Search the weekday array to convert the first weekday name into a column number from 0 to 6.

Step 7: Declare a calendar matrix with 6 rows and 7 columns and initialize day number to 1.

Step 8: Begin filling from the column of the first weekday in row 0.

Step 9: Move column by column, storing the day number and increasing it until the last day of the month is stored.

Step 10: When the column crosses Saturday, move to the next row and continue from Sunday.

Step 11: Print the heading, weekday names and every non-zero calendar cell with spacing.

Step 12: Stop.

Explanation:

The program separates the work into three logical parts: finding the number of days, converting the weekday name to a column number, and printing the calendar grid. This keeps the main method simple and makes each operation easy to test.

The month array stores names from January to December and the day array stores the corresponding number of days. February is corrected to 29 only if the year satisfies the leap-year condition: divisible by 400, or divisible by 4 but not by 100.

The weekday name is converted to a number where Sunday is 0, Monday is 1 and so on. This number decides how many blank cells are printed before day 1 of the month.

The calendar matrix has 6 rows and 7 columns because a month can spread over at most six weeks. The day counter starts from 1 and is placed into the matrix from the first weekday position. Once Saturday is crossed, filling continues in the next row from Sunday.

Calendar printing depends on knowing two things: the number of days in the month and the weekday of the first day. Once these are known, the program prints initial blank spaces before day 1 so that it appears under the correct weekday column. Then it prints dates one by one, moving to a new line after every seventh position. Month length may depend on leap year when February is involved. The core logic is therefore alignment and counting, not date searching.

Java Program:

Java
/**
* The class CalendarProgram inputs a year, month and the weekday name
* of the 1st day of that month and generates its calendar
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.util.*;
class CalendarProgram
{
    //Function to match the given month and return its maximum days
    int findMaxDay(String mname, int y)
    {
        String months[] = {"","January", "February", "March", "April", "May", "June",
            "July", "August", "September", "October", "November", "December"};
        int D[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

        if((y%400==0) || ((y%100!=0)&&(y%4==0)))
        {
            D[2]=29;
        }
        int max = 0;
        for(int i=1; i<=12; i++)
        {
            if(mname.equalsIgnoreCase(months[i]))
            {
                max = D[i];  //Saving maximum day of given month
            }
        }
        return max;
    }

    //Function to match the given weekday name and return its weekday no.
    int findDayNo(String wname)
    {
        String days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
            "Saturday"};
        int f = 0;
        for(int i=0; i<7; i++)
        {
            if(wname.equalsIgnoreCase(days[i]))
            {
                f = i;  //Saving week day no. given day (e.g. '0' for Sunday)
            }
        }
        return f;
    }

    //Function for creating the calendar
    void fillCalendar(int max, int f, String mname, int y)
    {
        int A[][] = new int[6][7];
        int x = 1, z = f;

        for(int i=0;i<6;i++)
        {
            for(int j=f; j<7; j++)
            {
                if(x<=max)
                {
                    A[i][j] = x;
                    x++;
                }
            }
            f = 0;
        }

        for(int j=0; j<z; j++) //Adjustment to bring last (6th) row elements to first row
        {
            A[0][j]=A[5][j];
        }

        printCalendar(A, mname, y); //Calling function to print the calendar
    }

    //Function for printing the calendar
    void printCalendar(int A[][], String mname, int y)
    {
        System.out.println("\n\t----------------------------------------------------");
        System.out.println("\t\t\t   "+mname+" "+y);
        System.out.println("\t----------------------------------------------------");
        System.out.println("\tSUN\tMON\tTUE\tWED\tTHU\tFRI\tSAT");
        System.out.println("\t----------------------------------------------------");

        for(int i = 0; i < 5; i++)
        {
            for(int j = 0; j < 7; j++)
            {
                if(A[i][j]!=0)
                System.out.print("\t "+A[i][j]);
                else
                System.out.print("\t ");
            }
            System.out.println("\n\t----------------------------------------------------");
        }
    }

    public static void main(String args[])
    {
        CalendarProgram ob = new CalendarProgram();
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the year : ");
        int y = sc.nextInt();
        System.out.print("Enter the month name (e.g. January) : ");
        String mname = sc.next();
        System.out.print("Enter the week day name (e.g. Sunday) of 1st day of "+mname+" : ");
        String wname = sc.next();

        int max = ob.findMaxDay(mname,y);
        int f = ob.findDayNo(wname);
        ob.fillCalendar(max,f,mname,y);
    }
}

Equivalent Python Program:

Python
def find_max_day(month, year):
    months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
    days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

    # Leap year correction is needed only for February.
    if year % 400 == 0 or (year % 100 != 0 and year % 4 == 0):
        days[1] = 29

    for i in range(12):
        if month.lower() == months[i].lower():
            return days[i]
    return 0


def find_day_no(weekday):
    days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

    # The index of the weekday becomes the calendar column number.
    for i in range(7):
        if weekday.lower() == days[i].lower():
            return i
    return -1


year = int(input("Enter the year: "))
month = input("Enter the month name: ")
weekday = input("Enter the weekday name of 1st day of " + month + ": ")

max_day = find_max_day(month, year)
first_day = find_day_no(weekday)

if max_day == 0 or first_day == -1:
    print("Invalid month or weekday name")
else:
    # Calendar matrix stores dates; 0 means the cell is blank.
    cal = [[0 for j in range(7)] for i in range(6)]
    day = 1
    row = 0
    col = first_day

    while day <= max_day:
        cal[row][col] = day
        day = day + 1
        col = col + 1

        # After Saturday, continue from Sunday in the next row.
        if col == 7:
            col = 0
            row = row + 1

    print("----------------------------------------------------")
    print("                 ", month, year)
    print("----------------------------------------------------")
    print("SUN\tMON\tTUE\tWED\tTHU\tFRI\tSAT")
    print("----------------------------------------------------")

    for i in range(6):
        for j in range(7):
            if cal[i][j] == 0:
                print("\t", end="")
            else:
                print(cal[i][j], "\t", end="")
        print()

Output:

Enter the year: 2016 Enter the month name: October Enter the weekday name of 1st day of October: Saturday ---------------------------------------------------- October 2016 ---------------------------------------------------- SUN MON TUE WED THU FRI SAT ---------------------------------------------------- 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

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →