A program that prints a one month calendar. The user specifies the number of days in the month and the day of the week on which the month begins.

PHOTO EMBED

Tue Oct 08 2024 01:13:38 GMT+0000 (Coordinated Universal Time)

Saved by @khadizasultana #c #loop

#include<stdio.h>
// Author- Khadiza Sultana
// Date- 10/8/2024
// Prints the Calendar of a month 
int main()
{
    int n, i, day; // n stores the number of days in the month, i used as a loop counter, day stores the starting day of the week(from 1 to 7, where 1= Sunday, 7= saturday)
    printf("Enter number of days in month:");
    scanf("%d", &n);
    printf("Enter starting day of the week (1=Sun, 7=Sat): ");
    scanf("%d", &day);
    for(i = 1; i < day; ++i) // This loop is responsible for printing spaces before the first day of the month.
    // If day == 1, no spaces are printed before thr month starts on sunday.
    // If day == 4, the loop runs three times and prints three spaces to align the first day under Wednesday
    {
        printf("   "); // three spacesto allign the days even when there's two digit days the days will be aligned 
    }
    for(i = 1; i <= n; i++, day++) // Prints the actual days of the month.
    // the day++ inside the for loop increments the day variable with each iteration, so the program knows when to start a new line after printing Saturday
    {
        printf("%2d ", i);
        if(day == 7)
        {
            day = 0;
            printf("\n");
        }
    }

    return 0;
}
content_copyCOPY

This program is not as hard as it seems to be. The most important part is a for statement that uses a variable i to count from 1 to n, where n is the last day in week; if so, it prints a new-line character.