C program that print given date calendar .

Develop a program that receives the month and year from the
keyboard as integers and prints the calendar in the following
format.  






Note that according to the Gregorian calendar 01/01/1900 was Monday. With this as the base the calendar should be generated.
#include<stdio.h>
#include<string.h>

int leap(int i){
    if(i%400==0) return 1;
    if(i%100==0) return 0;
    if(i%4==0)   return 1;
    return 0;
}

int day(int d,int m ,int y){
y=y-1;
int ty=0,i=1,ed=0,ly,me[]={999,0,3,3,6,1,4,6,2,5,0,3,5};
while(i*400<y) ty=i*400,i++;
if(y>=ty)ty=y-ty;
if(ty>=300)ty-=300,ed+=1;
if(ty>=200)ty-=200,ed+=3;
if(ty>=100)ty-=100,ed+=5;
ly=ty/4;
if(leap(y+1)&&m>2)ed+=1;//for feb 29 in current year



return (d+ly+ty+me[m]+ed)%7;
}
int cel(int d,int m,int y){
int ep,sp=day(d,m,y);  static int mday[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
ep=mday[m];
if(m==2&&leap(y))ep+=1;

for(int i=0,x=1;x<=ep;i++)
{  if(i%7==0)printf("\n\n\t");
    if(sp>i)printf(" \t");


    if(sp<=i)printf(" %d\t",x),x++,sp++;
}
}

int main()
{int y=1,m=1;
printf("\n\n\tEnter Zero for Exit ");
while(y&&m){
printf("\n\n\t\t\tEnter Month : ");
scanf("%d",&m);printf("\n\t\t\tEnter year : ");
scanf("%d",&y);
if(m&&y&&m<13)
{printf("\n\n\t Su\tMon\tTu\tWe\tTh\tFr\tSat\n");
    cel(1,m,y);}
else
    if(!(m&&y))
    printf("\n\n\tProgram exiting...\n\n");
    else printf("\n\n\tWrong Input try again...\n\n");
}
}

// i follow this video method:- https://youtu.be-zPW552asNY?t=8058
comment below if something went wrong
 

0 Comments