Basic Programming C Examples

WAP in C that receives the month & year as integers and prints the calendar

The Program in C that The Program in C that prints the sum of 9+99+999+..n terms given below:

#include <stdio.h>

int main() {
    int month, year, i, j, days, first_day;

    printf("Enter the month (1-12): ");
    scanf("%d", &month);
    printf("Enter the year: ");
    scanf("%d", &year);

    // Determine number of days in the month
    switch (month) {
        case 2:
            if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
                days = 29;
            } else {
                days = 28;
            }
            break;
        case 4: case 6: case 9: case 11:
            days = 30;
            break;
        default:
            days = 31;
    }

    // Determine the first day of the month
    // January and February are treated as months 13 and 14 of the previous year
    if (month == 1 || month == 2) {
        month += 12;
        year--;
    }
    first_day = (1 + (13 * (month + 1)) / 5 + year + (year / 4) - (year / 100) + (year / 400)) % 7;

    // Print the calendar
    printf("Sun Mon Tue Wed Thu Fri Sat\n");
    for (i = 0; i < first_day; i++) {
        printf("    ");
    }
    for (i = 1; i <= days; i++) {
        printf("%3d ", i);
        if ((first_day + i) % 7 == 0) {
            printf("\n");
        }
    }
    return 0;
}

Outputs:

Enter the month (1-12): 2
Enter the year: 2022
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

This program uses the input month and year from the user to determine the number of days in the month and the first day of the month.

It then uses a for loop to print the calendar for the specified month and year in the format “Sun Mon Tue Wed Thu Fri Sat”.

The program uses a switch statement to determine the number of days in the month, taking into account leap years for February.

It uses the algorithm called “Zeller’s congruence” to determine the first day of the month which is a mathematical formula that can be used to determine the day of the week for any date in the past, present or future.


It then uses nested for loop to print the calendar with proper format, first loop is used to align the dates and second loop is used to print the dates in the calendar.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

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

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X