0

I am trying to create the calendar program in Erlang , but I was not able to complete the logic of the code. And I tried the same calendar program in C language and its working but not accurate (i.e in the terms of days printing a/c to the Month and Year).

Tried Code :

#include<stdio.h>

int main()
{
  int year;
  int month, day,i;
  int days_in_month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
  char *months[]=
  {
    " ",
    "               January",
    "               February",
    "               March",
    "               April",
    "               May",
    "               June",
    "               July",
    "               August",
    "               September",
    "               October",
    "               November",
    "               December" };

  printf("Please enter a year: ");
  scanf(" %d",  &year);
  if(((year%4==0) && (year%100!=0)) || (year%400==0))
  {
      days_in_month[2] = 29;
  }
  else
  {
    days_in_month[2] = 28;
  }

for(i= 1; i<=12; i++)
{
 
    printf("%s", months[i]);
    printf(" Sun  Mon  Tue  Wed  Thu  Fri  Sat\n" );
    for ( day = 1; day <= 1; day++ )
    {
        printf(" ");
    }
   

    for ( day = 1; day <= days_in_month[i]; day++ )
    {
      
      (!(day % 7))?printf("%2d", day ):printf("%2d", day);


      if ( ( day ) % 7 > 0 ){
            printf("   " );
        }
      else{
            printf("\n " );
    }
    }
     printf("\n " );
}
return 0;
}

And the Example Output for the year (2020) and for one month:

                      December
 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
 29   30   31   
Allan Wind
  • 23,068
  • 5
  • 28
  • 38
  • 1
    This may be useful. [Zeller's congruence - Wikipedia](https://en.wikipedia.org/wiki/Zeller%27s_congruence) – MikeCAT Feb 03 '21 at 14:10
  • Sorry, but i do not see what is the problem here (except for Sundays getting printed first ) ? – TheEagle Feb 03 '21 at 14:18
  • But why don't you just use [C's standrad library functions](https://en.wikipedia.org/wiki/C_date_and_time_functions) for date and time manipulations and formatting ? – TheEagle Feb 03 '21 at 14:21
  • So far you're not doing anything to compute the day of the week on which the month begins. This is not an easy problem, but see the [linked duplicate question](https://stackoverflow.com/questions/51188738/suggestions-for-beginning-steps-to-build-a-calendar-program-in-c) for suggestions. – Steve Summit Feb 03 '21 at 14:32

0 Answers0