#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; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter