Write a program that prints a one-month calendar. The userspecifies the number of days in the month and the day of the weekon which the month begins. The numerical dates must line up sothat the rightmost digit of the date is aligned with the rightmostdigit of the date above or below it (if there are anysuch). Below is an example output showing theformat you should use. The user input is shown as underlinedtext.Enter number of days in month: 31Enter starting day of the week (1=Sun, 7=Sat): 3 1 2 3 4 56 7 8 9 10 11 1213 14 15 16 17 18 1920 21 22 23 24 25 2627 28 29 30 31This program isn’t as hard as it might seem. The mostimportant part is a for statement that uses a variable i to countfrom 1 to n, where n is the number of days in a month, printingeach value of i. Inside the loop, an if statement testswhether i is the last day in a week; if so, it prints anewline. Note that the two digit dates that start a week arealigned with the left margin.Here's what I have so far.#includeint main(){int i,days,start;printf("Enter number of days in month:",days);scanf("%d", &days);printf("Enter starting day of the week (1=Sun, 7=Sat):",start);scanf("%d", &start);for(i=1;i<=days;i++) { printf("%3d", i); if(i == 7 || i == 14 || i == 21 || i == 28) { printf("\n"); } }return 0;}

Engineering · College · Mon Jan 18 2021

Answered on

Here is a corrected and complete version of the calendar printing program. This program will align dates under each other correctly and will take into account the starting day:

```c #include

int main() { int days, start; printf("Enter number of days in month: "); scanf("%d", &days); printf("Enter starting day of the week (1=Sun, 7=Sat): "); scanf("%d", &start);

// Print the spaces for the first date for(int i = 0; i < start - 1; i++) { printf(" "); } // Print the days of the month for(int i = 1; i <= days; i++) { printf("%3d", i); // Print each day right aligned in a field of width 3 // Check if we need to go to the next line if ((start + i - 1) % 7 == 0) { printf("\n"); } } return 0; } ```

This code initializes 'days' and 'start', then it takes input from the user to fill these variables. It first prints the correct number of spaces for the starting day. Then, it prints each day of the month with proper alignment.

Moreover, it checks, after printing each day, if the next day is the start of a new week by checking if `(start + i - 1) % 7` is zero, which means the current day was the last day of the week and it should print a newline.

Extra: The program uses a few important C programming concepts:

1. `%d` in `scanf` and `printf`: This is a format specifier to work with integers. In `scanf()` it reads an integer from the input and stores it at the address of the variable provided. In `printf()` it prints an integer to the standard output.

2. `for` loop: This is used to run a block of code a specific number of times. The loop has a starting condition, an ending condition, and a way to increment the control variable. In the example, it's used to iterate through each day of the month.

3. `if` statement: This statement executes a block of code conditionally. Here, it is used to check if a newline character should be printed, indicating the end of a calendar week.

4. Right alignment with `printf`: By using `%3d`, we ensure that the digits are right-aligned in a field that's 3 characters wide. This helps to align the digits vertically when printed one under the other.

This program can be used to understand the flow of control using loops and conditionals, and to work with formatted console input/output, which are fundamental concepts in programming.

Related Questions