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.