How to Solve Various Problems on Calendar by C Language

Solve Various Problems on Calendar by C Programing Language

Introduction :

The calendar is very essential in our daily lives. The calendar is a fundamental tool which helps us for keeping track of dates, organizing schedules and events. Using different algorithms and techniques of C programming language, you can solve a wide range of Gregorian calendar-related problems.

In this article, I shall explore you how to solve various Gregorian calendar-related problems using the C programming language. Here, I shall cover the following Gregorian calendar-related topics which you can solve by C programing language. In the all programs, I use Gregorian calendar which represents dates using the day, month and year so the user has to enter the date according to the valid Gregorian calendar system.

What is calendar :

A calendar is used to organize and measure time and commonly used for planning and scheduling activities. It is a way to display the date into days, weeks, months and years. From the calendar, you can know information like important dates, public holidays, religious observances, public events, personal appointments and phases of the moon.

Type of calendar :

There are different types of calendar used in the world. They are Gregorian calendar, Islamic calendar, Hebrew calendar and Chinese calendar. According to cultural, religious or historical factors, different types of calendar have been created. The Gregorian is a most commonly used calendar in the world.

The Gregorian calendar is known as a solar calendar. It calculates based on the Earth’s revolution around the sun. There are 12 months. In this calendar each month has either 30 or 31 days. But in February has 28 days in an ordinary year and 29 days in leap year. Generally, you can be found Gregorian calendar in various formats such as paper-based wall calendar, desk calendar, digital calendar and online calendar of web application.

How run the following all programs :

You can run the following program base on Gregorian calendar problems in any type of a C compiler. I use the VS Code to run the all programs on my pc. If you want to install VS Code in your pc click here. Do you know how To Set Up VS Code For C And C++ on your pc. First open the VS Code (or any C compiler) in your pc and create a c file with .c extension. Now copy my below code and paste in your C file which you created just now. After that, save the file and run.

Checking if a Year is a leap year or not by C language :

The year which has 366 days instead of the 365 days is called leap year. There is an extra day added to February
(February 29th) of leap year in Gregorian calendar system. A leap year occurs every four years means the year is divisible by 4. But the year that is divisible by 100 but not by 400 is not a leap year. A leap year is divisible by 4 but not divisible by 100, except if it is also divisible by 400.

In this program, I shall explore how to check if a year is a leap year or not using the C programming language. In the main function, asked the user to enter a year and store it in the int “year” variable using the scanf() function. Next by if else statement check the year is a leap year or not. Here, I use the modulo operator (%) to check the condition for leap year. Then by printf() function, print the year is a leap year or not in the screen.

Code of the program :

You can copy the below code of checking leap year by C programming language and use it in your program.

/*Developed by Puskar Jasu*/
#include <stdio.h>
int main()
{
    int year;
    printf("Enter a year to check leap year or not ");
    scanf("%d", &year);
    if ((year % 100 == 0 && year % 400 == 0) || (year % 100 != 0 && year % 4 == 0))
        printf("The year %d is a leap year\n", year);
    else
        printf("The year %d is not a leap year\n", year);
}

Output :

When you run the above program on your pc, you can see the following output of determining if a Year is a leap year or not by C programing language.

check if a year is a leap year or not using the C programming language

Calculate how many days in a month of a year by C language :

When working with dates and calendars, you have to know how many days in a specific month of a year. The number of days in a month of a year can vary depending upon leap years and the position of the month within the year. The Gregorian calendar is the most popular and widely used calendar system in the world. There are 12 months in the Gregorian calendar. The days of each month is shown below.

  • January: 31 days
  • February: 28 days in common years and 29 days in leap years
  • March: 31 days
  • April: 30 days
  • May: 31 days
  • June: 30 days
  • July: 31 days
  • August: 31 days
  • September: 30 days
  • October: 31 days
  • November: 30 days
  • December: 31 days

In this program, I shall discuss with you how to find the number of days in a month of a year using the C programming language. In this program, first asked the user to enter the month and year which store in int variable such as “month” and “year” respectively. Then using if else statement, decide the number of days of a month and store in “day” variable. If the month is February and the year is a leap year, then days will be 29 otherwise 28. Here, I also store the name of the month in char array such as “month_name”. Finally, by printf() function prints the number of days of the given month and year on the screen.

Code of the program :

Just copy the below code and use in your own program.

/*Developed by Puskar Jasu*/
#include <stdio.h>
#include <string.h>
int main()
{
    int year, month, day;
    char month_name[10];
    printf("Enter month for count the number of days\nsuch as 1 for january, 2 for february so on. ");
    scanf("%d", &month);
    printf("Enter year for count the number of days ");
    scanf("%d", &year);
    if (month == 1)
    {
        day = 31;
        strcpy(month_name, "January");
    }
    else if (month == 2)
    {
        if ((year % 100 == 0 && year % 400 == 0) || (year % 100 != 0 && year % 4 == 0))
            day = 29;
        else
            day = 28;
        strcpy(month_name, "February");
    }
    else if (month == 3)
    {
        day = 31;
        strcpy(month_name, "March");
    }
    else if (month == 4)
    {
        day = 30;
        strcpy(month_name, "April");
    }
    else if (month == 5)
    {
        day = 31;
        strcpy(month_name, "May");
    }
    else if (month == 6)
    {
        day = 30;
        strcpy(month_name, "June");
    }
    else if (month == 7)
    {
        day = 31;
        strcpy(month_name, "July");
    }
    else if (month == 8)
    {
        day = 31;
        strcpy(month_name, "August");
    }
    else if (month == 9)
    {
        day = 30;
        strcpy(month_name, "September");
    }
    else if (month == 10)
    {
        day = 31;
        strcpy(month_name, "October");
    }
    else if (month == 11)
    {
        day = 30;
        strcpy(month_name, "November");
    }
    else if (month == 12)
    {
        day = 31;
        strcpy(month_name, "December");
    }
    printf("The days of %s %d is %d\n", month_name, year, day);
}

Output :

After running the program, you can see the following output of calculating how many days in a month of a year by C programing language.

Calculate how many days in a month of a year by C programing language

Find the day of the week for a given date by C language :

Find the day of the week for a particular date is valuable for some applications. When you create a calendar application, scheduling events, organizing data or any other date-dependent application, you have to know how to find the day from a given date. There are seven days in a week in the Gregorian calendar. They are Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday.

In this program, I show you how to find the day of the week for a specific date using the C programming language. At the beginning of the program, ask the user input day, month and year of given date which store in “day”, “month” and “year” int variables respectively. Then check the year leap year or not for deciding the days of February. The days of February store in “f_days” variable.

Next, using if else statement add all days of earlier months of input month in “t_days” variable. After that, calculate the odd day by mathematical calculation where 0 represents Sunday, 1 represents Monday and so on. At last, store the name of day in char array such as “day_name” according to odd day. Finally, by printf() function print the day of the week of the given date on the screen.

Code of the program :

You can copy the following code and use it in your program.

/*Developed by Puskar Jasu*/
#include <stdio.h>
#include <string.h>
int main()
{
    int year, month, day, f_days, t_days, i;
    char day_name[10];
    printf("Enter days of the date ");
    scanf("%d", &day);
    printf("Enter month of the date ");
    scanf("%d", &month);
    printf("Enter year of the date ");
    scanf("%d", &year);
    if (((year % 100 == 0) && (year % 400 == 0)) || ((year % 100 != 0) && (year % 4 == 0)))
        f_days = 29;
    else
        f_days = 28;
    if (month == 1)
        t_days = 0;
    else if (month == 2)
        t_days = 31;
    else if (month == 3)
        t_days = 31 + f_days;
    else if (month == 4)
        t_days = 31 + f_days + 31;
    else if (month == 5)
        t_days = 31 + f_days + 31 + 30;
    else if (month == 6)
        t_days = 31 + f_days + 31 + 30 + 31;
    else if (month == 7)
        t_days = 31 + f_days + 31 + 30 + 31 + 30;
    else if (month == 8)
        t_days = 31 + f_days + 31 + 30 + 31 + 30 + 31;
    else if (month == 9)
        t_days = 31 + f_days + 31 + 30 + 31 + 30 + 31 + 31;
    else if (month == 10)
        t_days = 31 + f_days + 31 + 30 + 31 + 30 + 31 + 31 + 30;
    else if (month == 11)
        t_days = 31 + f_days + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
    else if (month == 12)
        t_days = 31 + f_days + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;
    i = ((year - 1) + (year - 1) / 4 + (year - 1) / 400 - (year - 1) / 100 + t_days + day) % 7;
    if (i == 1)
        strcpy(day_name, "Monday");
    else if (i == 2)
        strcpy(day_name, "Tuesday");
    else if (i == 3)
        strcpy(day_name, "Wednesday");
    else if (i == 4)
        strcpy(day_name, "Thusday");
    else if (i == 5)
        strcpy(day_name, "Friday");
    else if (i == 6)
        strcpy(day_name, "Saturday");
    else
        strcpy(day_name, "Sunday");
    printf("The day of %d/%d/%d is %s\n", day, month, year, day_name);
    return 0;
}

Output :

You can see the below output of find the day of the week for a given date by C programing language when you run the program on your pc.

find the day of the week for a specific date using the C programming language

Determine the day of the week for the first day of a month by C language :

For generating calendars and organizing the monthly schedule, you have to find the day of the week for the starting day of a given month. In this program, I show you how to determine the day of the week for the first day of a month by C programing language.

At the beginning of the program, ask the user to enter the month and year of given date which store in “month” and “year” int variables respectively. Then check the year leap year or not for deciding the days of February. The days of February store in “f_days” variable.

Next, using if else statement add all days of earlier months of input month in “t_days” variable and also store month name in “month_name” char array. After that, calculate the odd day for the first day of month by mathematical calculation where 0 represents Sunday, 1 represents Monday and so on. At last, store the name of day in char array such as “day_name” according to odd day. Finally, by printf() function print first day of month of a given month in the screen.

Code of the program :

You can copy the below code and use in your own program.

/*Developed by Puskar Jasu*/
#include <stdio.h>
#include <string.h>
int main()
{
    int year, month, f_days, t_days, i;
    char day_name[10];
    char month_name[10];
    printf("Enter month of the date ");
    scanf("%d", &month);
    printf("Enter year of the date ");
    scanf("%d", &year);
    if (((year % 100 == 0) && (year % 400 == 0)) || ((year % 100 != 0) && (year % 4 == 0)))
        f_days = 29;
    else
        f_days = 28;
    if (month == 1)
    {
        t_days = 0;
        strcpy(month_name, "January");
    }
    else if (month == 2)
    {
        t_days = 31;
        strcpy(month_name, "February");
    }
    else if (month == 3)
    {
        t_days = 31 + f_days;
        strcpy(month_name, "March");
    }
    else if (month == 4)
    {
        t_days = 31 + f_days + 31;
        strcpy(month_name, "April");
    }
    else if (month == 5)
    {
        t_days = 31 + f_days + 31 + 30;
        strcpy(month_name, "May");
    }
    else if (month == 6)
    {
        t_days = 31 + f_days + 31 + 30 + 31;
        strcpy(month_name, "June");
    }
    else if (month == 7)
    {
        t_days = 31 + f_days + 31 + 30 + 31 + 30;
        strcpy(month_name, "July");
    }
    else if (month == 8)
    {
        t_days = 31 + f_days + 31 + 30 + 31 + 30 + 31;
        strcpy(month_name, "August");
    }
    else if (month == 9)
    {
        t_days = 31 + f_days + 31 + 30 + 31 + 30 + 31 + 31;
        strcpy(month_name, "September");
    }
    else if (month == 10)
    {
        t_days = 31 + f_days + 31 + 30 + 31 + 30 + 31 + 31 + 30;
        strcpy(month_name, "October");
    }
    else if (month == 11)
    {
        t_days = 31 + f_days + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
        strcpy(month_name, "November");
    }
    else if (month == 12)
    {
        t_days = 31 + f_days + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;
        strcpy(month_name, "December");
    }
    i = ((year - 1) + (year - 1) / 4 + (year - 1) / 400 - (year - 1) / 100 + t_days + 1) % 7;
    if (i == 1)
        strcpy(day_name, "Monday");
    else if (i == 2)
        strcpy(day_name, "Tuesday");
    else if (i == 3)
        strcpy(day_name, "Wednesday");
    else if (i == 4)
        strcpy(day_name, "Thusday");
    else if (i == 5)
        strcpy(day_name, "Friday");
    else if (i == 6)
        strcpy(day_name, "Saturday");
    else
        strcpy(day_name, "Sunday");
    printf("The day of the first day of %s %d is %s\n", month_name, year, day_name);
    return 0;
}

Output :

After running this above program, the following output of find the day of the week for the first day of a month by C programing language will be displayed on the screen.

determine the day of the week for the first day of a month by C programing language

Calculate the number of days between two dates by C language :

Sometimes, you have to calculate the number of days between two dates for working with dates and calendars. Calculating the difference between two dates is a common requirement in various programming applications such as age calculation, calculate duration between events or any other date-based applications. In this program, I shall show you how to calculate the difference between two dates using the C programming language.

At first store the days of each month in “month_day” int array. After that, asked the user for entering day, month and year of first date which store in “day1”, “month1” and “year1” int variables respectively. Also asked day, month and year for second date which store in “day2”, “month2” and “year2” variables respectively. Then, in the if else statement, calculate the difference of two dates and store in “total_days” variable. Finally, display the number of days between two dates using printf() function in the screen.

Code of the program :

The following code is used for calculating the number of days between two dates using the C programming language.

/*Developed by Puskar Jasu*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
  int year1, month1, day1, year2, month2, day2, f_days, total_days = 0, i = 0, j = 0;
  //char day_name[10];
  int month_day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  printf("Enter days of first date ");
  scanf("%d", &day1);
  printf("Enter month of first date ");
  scanf("%d", &month1);
  printf("Enter year of first date ");
  scanf("%d", &year1);
  printf("Enter days of second date ");
  scanf("%d", &day2);
  printf("Enter month of second date ");
  scanf("%d", &month2);
  printf("Enter year of second date ");
  scanf("%d", &year2);
  if (year1 == year2)
  {
    if (month1 == month2)
    {
      total_days = abs(day2 - day1);
    }
    else if (month1 < month2)
    {
      for (i = month1; i < month2; i++)
      {
        total_days = total_days + month_day[i - 1];
      }
      if (month1 <= 2&& month2>=3)
      {
        if ((year1 % 100 == 0 && year1 % 400 == 0) || (year1 % 100 != 0 && year1 % 4 == 0))
        {
          total_days = total_days + 1;
        }
      }
      total_days = total_days + day2 - day1;
    }
    else
    {
      for (i = month2; i < month1; i++)
      {
        total_days = total_days + month_day[i - 1];
      }
      if (month2 <= 2 && month1 > 2)
      {
        if ((year1 % 100 == 0 && year1 % 400 == 0) || (year1 % 100 != 0 && year1 % 4 == 0))
        {
          total_days = total_days + 1;
        }
      }
      total_days = total_days + day1 - day2;
    }
  }
  else if (year1 < year2)
  {
    if (month1 == month2)
    {
      total_days = (year2 - year1) * 365;
      for (i = year1; i <= year2; i++)
      {
        if ((i % 100 == 0 && i % 400 == 0) || (i % 100 != 0 && i % 4 == 0))
        {
          total_days = total_days + 1;
        }
      }
      if (month1 >= 3 && ((year1 % 100 == 0 && year1 % 400 == 0) || (year1 % 100 != 0 && year1 % 4 == 0)))
      {
        total_days = total_days - 1;
      }
      if (month2 <= 2 && ((year2 % 100 == 0 && year2 % 400 == 0) || (year2 % 100 != 0 && year2 % 4 == 0)))
      {
        total_days = total_days - 1;
      }
      total_days = total_days + day2 - day1;
    }
    else if (month1 < month2)
    {
      total_days = (year2 - year1) * 365;
      for (i = month1; i < month2; i++)
      {
        total_days = total_days + month_day[i - 1];
      }
      for (i = year1; i <= year2; i++)
      {
        if ((i % 100 == 0 && i % 400 == 0) || (i % 100 != 0 && i % 4 == 0))
        {
          total_days = total_days + 1;
        }
      }
      if (month1 >= 3 && ((year1 % 100 == 0 && year1 % 400 == 0) || (year1 % 100 != 0 && year1 % 4 == 0)))
      {
        total_days = total_days - 1;
      }
      if (month2 <= 2 && ((year2 % 100 == 0 && year2 % 400 == 0) || (year2 % 100 != 0 && year2 % 4 == 0)))
      {
        total_days = total_days - 1;
      }
      total_days = total_days + day2 - day1;
    }
    else
    {
      total_days = (year2 - year1) * 365;
      for (i = month2; i < month1; i++)
      {
        total_days = total_days - month_day[i - 1];
      }
      for (i = year1; i <= year2; i++)
      {
        if ((i % 100 == 0 && i % 400 == 0) || (i % 100 != 0 && i % 4 == 0))
        {
          total_days = total_days + 1;
        }
      }
      if (month1 >= 3 && ((year1 % 100 == 0 && year1 % 400 == 0) || (year1 % 100 != 0 && year1 % 4 == 0)))
      {
        total_days = total_days - 1;
      }
      if (month2 <= 2 && ((year2 % 100 == 0 && year2 % 400 == 0) || (year2 % 100 != 0 && year2 % 4 == 0)))
      {
        total_days = total_days - 1;
      }
      total_days = total_days + day2 - day1;
    }
  }
  else
  {
    if (month1 == month2)
    {
      total_days = (year1 - year2) * 365;
      for (i = year2; i <= year1; i++)
      {
        if ((i % 100 == 0 && i % 400 == 0) || (i % 100 != 0 && i % 4 == 0))
        {
          total_days = total_days + 1;
        }
      }
      if (month1 >= 3 && ((year2 % 100 == 0 && year2 % 400 == 0) || (year2 % 100 != 0 && year2 % 4 == 0)))
      {
        total_days = total_days - 1;
      }
      if (month2 <= 2 && ((year1 % 100 == 0 && year1 % 400 == 0) || (year1 % 100 != 0 && year1 % 4 == 0)))
      {
        total_days = total_days - 1;
      }
      total_days = total_days - day2 + day1;
    }
    else if (month1 < month2)
    {
      total_days = (year1 - year2) * 365;
      for (i = month1; i < month2; i++)
      {
        total_days = total_days - month_day[i - 1];
      }
      for (i = year2; i <= year1; i++)
      {
        if ((i % 100 == 0 && i % 400 == 0) || (i % 100 != 0 && i % 4 == 0))
        {
          total_days = total_days + 1;
        }
      }
      if (month1 <= 2 && ((year1 % 100 == 0 && year1 % 400 == 0) || (year1 % 100 != 0 && year1 % 4 == 0)))
      {
        total_days = total_days - 1;
      }
      if (month2 >= 3 && ((year2 % 100 == 0 && year2 % 400 == 0) || (year2 % 100 != 0 && year2 % 4 == 0)))
      {
        total_days = total_days - 1;
      }
      total_days = total_days - day2 + day1;
    }
    else
    {
      total_days = (year1 - year2) * 365;
      for (i = month2; i < month1; i++)
      {
        total_days = total_days + month_day[i - 1];
      }
      for (i = year2; i <=year1; i++)
      {
        if ((i % 100 == 0 && i % 400 == 0) || (i % 100 != 0 && i % 4 == 0))
        {
          total_days = total_days + 1;
        }
      }
      if (month2 >= 3 && ((year2 % 100 == 0 && year2 % 400 == 0) || (year2 % 100 != 0 && year2 % 4 == 0)))
      {
        total_days = total_days - 1;
      }
      if (month1 <= 2 && ((year1 % 100 == 0 && year1 % 400 == 0) || (year1 % 100 != 0 && year1 % 4 == 0)))
      {
        total_days = total_days - 1;
      }
      total_days = total_days - day2 + day1;
    }
  }
  printf("The different between %d/%d/%d and %d/%d/%d is %d days\n",day1,month1,year1,day2,month2,year2, total_days);
  return 0;
}

Output :

Just copy the above program and run on your pc. You can see the output of the difference between two dates like below image.

calculate the difference between two dates using the C programming language

Find day of week of a date compare to another date by C language :

Find the day of the week for a specific date compared to another date is very common and useful in the programming world. Calculate which day of the week of a particular date falls on compare to another date use for various date-related application. In this program, I show you how to find the day of the week for a date compare to another date using C programming language.

At first store the days of each month in “month_day” int array. After that, asked the user for entering day, month and year of first date which store in “day1”, “month1” and “year1” int variables respectively. Then ask the user to enter day of the week of the given date and store in “given_odd_day” variable. Also asked for day, month and year of second date which store in “day2”, “month2” and “year2” variables respectively.

Then, in the if else statement, calculate the difference of two dates and store in “total_days” variable. Now with the help of different of two dates and given odd day, calculate the odd day of second date which store in “calculate_odd_day” variable. After that, store the names of the day of given and calculated dates in “day_name1” and “day_name2” variables respectively. Finally, display the name of the day of week of two dates using printf() function in the screen.

Code of the program :

Now copy the code and use in your program.

/*Developed by Puskar Jasu*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
  int year1, month1, day1, year2, month2, day2, f_days, total_days = 0, i = 0, given_odd_day, calculate_odd_day;
  char day_name1[10];
  char day_name2[10];
  int month_day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  printf("Enter days of given date ");
  scanf("%d", &day1);
  printf("Enter month of given date ");
  scanf("%d", &month1);
  printf("Enter year of given date ");
  scanf("%d", &year1);
  printf("Enter day of week of given date\n0 for sunday\n1 for monday\n2 for tuesday\n3 for wednesday\n4 for thusday\n5 for friday\n6 for saturday ");
  scanf("%d", &given_odd_day);
  printf("Enter days of date for find day of week ");
  scanf("%d", &day2);
  printf("Enter month of date for find day of week ");
  scanf("%d", &month2);
  printf("Enter year of date for find day of week ");
  scanf("%d", &year2);
  if (year1 == year2)
  {
    if (month1 == month2)
    {
      total_days = abs(day2 - day1);
    }
    else if (month1 < month2)
    {
      for (i = month1; i < month2; i++)
      {
        total_days = total_days + month_day[i - 1];
      }
      if (month1 <= 2 && month2 >= 3)
      {
        if ((year1 % 100 == 0 && year1 % 400 == 0) || (year1 % 100 != 0 && year1 % 4 == 0))
        {
          total_days = total_days + 1;
        }
      }
      total_days = total_days + day2 - day1;
    }
    else
    {
      for (i = month2; i < month1; i++)
      {
        total_days = total_days + month_day[i - 1];
      }
      if (month2 <= 2 && month1 > 2)
      {
        if ((year1 % 100 == 0 && year1 % 400 == 0) || (year1 % 100 != 0 && year1 % 4 == 0))
        {
          total_days = total_days + 1;
        }
      }
      total_days = total_days + day1 - day2;
    }
  }
  else if (year1 < year2)
  {
    if (month1 == month2)
    {
      total_days = (year2 - year1) * 365;
      for (i = year1; i <= year2; i++)
      {
        if ((i % 100 == 0 && i % 400 == 0) || (i % 100 != 0 && i % 4 == 0))
        {
          total_days = total_days + 1;
        }
      }
      if (month1 >= 3 && ((year1 % 100 == 0 && year1 % 400 == 0) || (year1 % 100 != 0 && year1 % 4 == 0)))
      {
        total_days = total_days - 1;
      }
      if (month2 <= 2 && ((year2 % 100 == 0 && year2 % 400 == 0) || (year2 % 100 != 0 && year2 % 4 == 0)))
      {
        total_days = total_days - 1;
      }
      total_days = total_days + day2 - day1;
    }
    else if (month1 < month2)
    {
      total_days = (year2 - year1) * 365;
      for (i = month1; i < month2; i++)
      {
        total_days = total_days + month_day[i - 1];
      }
      for (i = year1; i <= year2; i++)
      {
        if ((i % 100 == 0 && i % 400 == 0) || (i % 100 != 0 && i % 4 == 0))
        {
          total_days = total_days + 1;
        }
      }
      if (month1 >= 3 && ((year1 % 100 == 0 && year1 % 400 == 0) || (year1 % 100 != 0 && year1 % 4 == 0)))
      {
        total_days = total_days - 1;
      }
      if (month2 <= 2 && ((year2 % 100 == 0 && year2 % 400 == 0) || (year2 % 100 != 0 && year2 % 4 == 0)))
      {
        total_days = total_days - 1;
      }
      total_days = total_days + day2 - day1;
    }
    else
    {
      total_days = (year2 - year1) * 365;
      for (i = month2; i < month1; i++)
      {
        total_days = total_days - month_day[i - 1];
      }
      for (i = year1; i <= year2; i++)
      {
        if ((i % 100 == 0 && i % 400 == 0) || (i % 100 != 0 && i % 4 == 0))
        {
          total_days = total_days + 1;
        }
      }
      if (month1 >= 3 && ((year1 % 100 == 0 && year1 % 400 == 0) || (year1 % 100 != 0 && year1 % 4 == 0)))
      {
        total_days = total_days - 1;
      }
      if (month2 <= 2 && ((year2 % 100 == 0 && year2 % 400 == 0) || (year2 % 100 != 0 && year2 % 4 == 0)))
      {
        total_days = total_days - 1;
      }
      total_days = total_days + day2 - day1;
    }
  }
  else
  {
    if (month1 == month2)
    {
      total_days = (year1 - year2) * 365;
      for (i = year2; i <= year1; i++)
      {
        if ((i % 100 == 0 && i % 400 == 0) || (i % 100 != 0 && i % 4 == 0))
        {
          total_days = total_days + 1;
        }
      }
      if (month1 >= 3 && ((year2 % 100 == 0 && year2 % 400 == 0) || (year2 % 100 != 0 && year2 % 4 == 0)))
      {
        total_days = total_days - 1;
      }
      if (month2 <= 2 && ((year1 % 100 == 0 && year1 % 400 == 0) || (year1 % 100 != 0 && year1 % 4 == 0)))
      {
        total_days = total_days - 1;
      }
      total_days = total_days - day2 + day1;
    }
    else if (month1 < month2)
    {
      total_days = (year1 - year2) * 365;
      for (i = month1; i < month2; i++)
      {
        total_days = total_days - month_day[i - 1];
      }
      for (i = year2; i <= year1; i++)
      {
        if ((i % 100 == 0 && i % 400 == 0) || (i % 100 != 0 && i % 4 == 0))
        {
          total_days = total_days + 1;
        }
      }
      if (month1 <= 2 && ((year1 % 100 == 0 && year1 % 400 == 0) || (year1 % 100 != 0 && year1 % 4 == 0)))
      {
        total_days = total_days - 1;
      }
      if (month2 >= 3 && ((year2 % 100 == 0 && year2 % 400 == 0) || (year2 % 100 != 0 && year2 % 4 == 0)))
      {
        total_days = total_days - 1;
      }
      total_days = total_days - day2 + day1;
    }
    else
    {
      total_days = (year1 - year2) * 365;
      for (i = month2; i < month1; i++)
      {
        total_days = total_days + month_day[i - 1];
      }
      for (i = year2; i <= year1; i++)
      {
        if ((i % 100 == 0 && i % 400 == 0) || (i % 100 != 0 && i % 4 == 0))
        {
          total_days = total_days + 1;
        }
      }
      if (month2 >= 3 && ((year2 % 100 == 0 && year2 % 400 == 0) || (year2 % 100 != 0 && year2 % 4 == 0)))
      {
        total_days = total_days - 1;
      }
      if (month1 <= 2 && ((year1 % 100 == 0 && year1 % 400 == 0) || (year1 % 100 != 0 && year1 % 4 == 0)))
      {
        total_days = total_days - 1;
      }
      total_days = total_days - day2 + day1;
    }
  }
  if ((day1 > day2 & month1 == month2 && year1 == year2) || (month1 > month2 && year1 == year2) || year1 > year2)
  {
    calculate_odd_day = 7 - (total_days + 7 - given_odd_day) % 7;
  }
  else
  {
    calculate_odd_day = (total_days + given_odd_day) % 7;
  }
  if (given_odd_day == 1)
    strcpy(day_name1, "Monday");
  else if (given_odd_day == 2)
    strcpy(day_name1, "Tuesday");
  else if (given_odd_day == 3)
    strcpy(day_name1, "Wednesday");
  else if (given_odd_day == 4)
    strcpy(day_name1, "Thusday");
  else if (given_odd_day == 5)
    strcpy(day_name1, "Friday");
  else if (given_odd_day == 6)
    strcpy(day_name1, "Saturday");
  else
    strcpy(day_name1, "Sunday");
  if (calculate_odd_day == 1)
    strcpy(day_name2, "Monday");
  else if (calculate_odd_day == 2)
    strcpy(day_name2, "Tuesday");
  else if (calculate_odd_day == 3)
    strcpy(day_name2, "Wednesday");
  else if (calculate_odd_day == 4)
    strcpy(day_name2, "Thusday");
  else if (calculate_odd_day == 5)
    strcpy(day_name2, "Friday");
  else if (calculate_odd_day == 6)
    strcpy(day_name2, "Saturday");
  else
    strcpy(day_name2, "Sunday");
  printf("If the day of week of %d/%d/%d is %s\nthen the day of week of %d/%d/%d is %s\n", day1, month1, year1, day_name1, day2, month2, year2, day_name2);
  return 0;
}

Output :

You can see the following output of find day of week of a date compare to another date on your pc by C programming language.

find the day of the week for a date compare to another date using C programming language

Generate and print a calendar for a specific month by C language :

Calendar is very useful in our normal life by which we see dates, plan events and perform various date-related tasks. You can generate calendars and display the days of a particular month in an organized manner by C programming language. In this program, I show you how you generate a calendar for given month of year using C programming language.

Here, I create a calendar which display the days of the month in a grid format with the spacing and indentation for proper alignment by C programming language. To create an organized calendar you have to calculate the day of the week for the first day of the month and the number of days in the given month.

At the beginning of the program, asked the user for the month and year for generating the calendar. The value of month and year store in “month” and “year” variables respectively. Next, check the year is a leap year or not. The days of February are 29 if the year is a leap year, otherwise 28. Then using if else statement, calculate odd day for finding the first day of the month. At the same time, store the days of the month and name of the month in “month_day” and “month_name” variables respectively.

After that, using printf() function display the given month and year at the top of the calendar and the names of the days of the week (Sunday, Monday, Tuesday etc.) as column headers with proper spacing and indentation. Now store the all day of the given month in “arr” array as the first and total days of the given month according to the Gregorian calendar with “for” loop. Finally, using printf() function print the generated calendar on the screen with the help of “for” loop.

Code of the program :

You can copy the below code for your own program.

/*Developed by Puskar Jasu*/
#include <stdio.h>
#include <string.h>
int main()
{
  int year, month, i, j, k = 1, l = 0, f_days, t_days, odd_day, month_day;
  int arr[5][7];
  char month_name[10];
  printf("Enter month for generate calendar ");
  scanf("%d", &month);
  printf("Enter year for generate calendar ");
  scanf("%d", &year);
  if (((year % 100 == 0) && (year % 400 == 0)) || ((year % 100 != 0) && (year % 4 == 0)))
    f_days = 29;
  else
    f_days = 28;
  if (month == 1)
  {
    t_days = 0;
    month_day = 31;
    strcpy(month_name, "January");
  }
  else if (month == 2)
  {
    t_days = 31;
    month_day = f_days;
    strcpy(month_name, "February");
  }
  else if (month == 3)
  {
    t_days = 31 + f_days;
    month_day = 31;
    strcpy(month_name, "March");
  }
  else if (month == 4)
  {
    t_days = 31 + f_days + 31;
    month_day = 30;
    strcpy(month_name, "April");
  }
  else if (month == 5)
  {
    t_days = 31 + f_days + 31 + 30;
    month_day = 31;
    strcpy(month_name, "May");
  }
  else if (month == 6)
  {
    t_days = 31 + f_days + 31 + 30 + 31;
    month_day = 30;
    strcpy(month_name, "June");
  }
  else if (month == 7)
  {
    t_days = 31 + f_days + 31 + 30 + 31 + 30;
    month_day = 31;
    strcpy(month_name, "July");
  }
  else if (month == 8)
  {
    t_days = 31 + f_days + 31 + 30 + 31 + 30 + 31;
    month_day = 31;
    strcpy(month_name, "August");
  }
  else if (month == 9)
  {
    t_days = 31 + f_days + 31 + 30 + 31 + 30 + 31 + 31;
    month_day = 30;
    strcpy(month_name, "September");
  }
  else if (month == 10)
  {
    t_days = 31 + f_days + 31 + 30 + 31 + 30 + 31 + 31 + 30;
    month_day = 31;
    strcpy(month_name, "October");
  }
  else if (month == 11)
  {
    t_days = 31 + f_days + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
    month_day = 30;
    strcpy(month_name, "November");
  }
  else if (month == 12)
  {
    t_days = 31 + f_days + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;
    month_day = 31;
    strcpy(month_name, "December");
  }
  odd_day = ((year - 1) + (year - 1) / 4 + (year - 1) / 400 - (year - 1) / 100 + t_days + 1) % 7;
  printf("           %s %d\n", month_name, year);
  printf("Sun  Mon  Tue  Wed  Thus  Fri  Sat\n");
  for (i = 0; i < 6; i++)
  {
    for (j = 0; j < 7; j++)
    {
      if (l < odd_day)
      {
        arr[i][j] = 0;
      }
      else
      {
        arr[i][j] = k;
        if (k > month_day)
          arr[i][j] = 0;
        k++;
      }
      l++;
    }
  }
  arr[5][3] = 0;
  arr[5][6] = 0;
  for (i = 0; i < 6; i++)
  {
    for (j = 0; j < 7; j++)
    {
      if (arr[i][j] < 10)
      {
        if (arr[i][j] == 0)
          printf("     ");
        else
          printf(" %d   ", arr[i][j]);
      }
      else
        printf(" %d  ", arr[i][j]);
      k++;
    }
    printf("\n");
  }
  return 0;
}

Output :

After running this program on your pc, you can see the generated calendar like below image by C programming language.

generate a calendar for given month of year using C programming language

Conclusion :

There are different type of calendar-related problems you can solve using the C programming language such as checking leap years, determining the day of the week, calculating the number of days between dates and generating calendars etc. In the above article, I discuss about various calendar-related problems which you can solve using C programming language. You can use my code in your program. Thank you for visiting my site.

Scroll to Top