C-Program for calendar
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int leap(int year) //if leap year we have to reduce one day
{
if((year%4==0&&year%100!=0)||(year%100==0&&year%400==0))
return -1;
return 0;
}
int main(int argc,char *argv)
{
char *date=(char *)malloc(100);
int day;
int month;
int year;
int temp;
int century[4]={4,2,0,6};
int centurycode;
printf("\n enter the date,month and year in the following format");
printf("\n dd/mm/yyyy");
scanf("%s",date);
sscanf(date,"%d%*c%d%*c%d",&day,&month,&year);
/* Error Checking*/
if(month>12 || day >31)
{
printf("in valid date");
return;
}
if(month==2)
{
if(leap(year)&&day>29){
printf("invalid date");return;}
if(day>28){
printf("invalid date");return;}
}
if(month==4||month==9||month==6||month==11)
{
if(day>30){
printf("\ninvalid date");return;}
}
else
{
if(day>31){
printf("\n invalid date");return;}
}
/*Step1: Take only two digits of the year*/
int rem;
int two=0,i=0;
temp=year;
while(temp>100)
{
rem=temp%10;
two=two+rem*pow(10,i++);
temp=temp/10;
}
/*Step2:Year/4*/
/*Month code*/
enum {
JAN=0,
FEB=3,
MAR=3,
APR=6,
MAY=1,
JUN=4,
JUL=6,
AUG=2,
SEP=5,
OCT=0,
NOV=3,
DEC=5,
};
/*MONTH */
switch(month)
{
case 1:month=JAN;break;
case 2:month=FEB;break;
case 3:month=MAR;break;
case 4:month=APR;break;
case 5:month=MAY;break;
case 6:month=JUN;break;
case 7:month=JUL;break;
case 8:month=AUG;break;
case 9:month=SEP;break;
case 10:month=OCT;break;
case 11:month=NOV;break;
case 12:month=DEC;break;
}
/* Generating century code */
int cent=1;
int add=1,j=0;
int cent_code=century[j];
while(cent!=year)
{
cent=cent+add;
//printf("%d",cent);
if(!(cent%100))
{
if(j>3)
j=0;
cent_code=century[j++];
}
}
/* Calculation=DAY+Last Two digits of YEAR+YEAR/4+MONTHCODE+CENTURYCODE
CALCULATION/7.The remainder will be the result*/
int calc;
calc=day+two+(two/4)+month+cent_code;
calc=calc%7;
switch(calc)
{
case 0: printf("\n Its Sunday");break;
case 1: printf("\n Its Monday");break;
case 2: printf("\n Its Tuesday");break;
case 3: printf("\n Its Wednesday");break;
case 4: printf("\n Its Thursday"); break;
case 5: printf("\n Its Friday"); break;
case 6: printf("\n Its Saturday"); break;
}
return 0;
}
#include<stdlib.h>
#include<math.h>
int leap(int year) //if leap year we have to reduce one day
{
if((year%4==0&&year%100!=0)||(year%100==0&&year%400==0))
return -1;
return 0;
}
int main(int argc,char *argv)
{
char *date=(char *)malloc(100);
int day;
int month;
int year;
int temp;
int century[4]={4,2,0,6};
int centurycode;
printf("\n enter the date,month and year in the following format");
printf("\n dd/mm/yyyy");
scanf("%s",date);
sscanf(date,"%d%*c%d%*c%d",&day,&month,&year);
/* Error Checking*/
if(month>12 || day >31)
{
printf("in valid date");
return;
}
if(month==2)
{
if(leap(year)&&day>29){
printf("invalid date");return;}
if(day>28){
printf("invalid date");return;}
}
if(month==4||month==9||month==6||month==11)
{
if(day>30){
printf("\ninvalid date");return;}
}
else
{
if(day>31){
printf("\n invalid date");return;}
}
/*Step1: Take only two digits of the year*/
int rem;
int two=0,i=0;
temp=year;
while(temp>100)
{
rem=temp%10;
two=two+rem*pow(10,i++);
temp=temp/10;
}
/*Step2:Year/4*/
/*Month code*/
enum {
JAN=0,
FEB=3,
MAR=3,
APR=6,
MAY=1,
JUN=4,
JUL=6,
AUG=2,
SEP=5,
OCT=0,
NOV=3,
DEC=5,
};
/*MONTH */
switch(month)
{
case 1:month=JAN;break;
case 2:month=FEB;break;
case 3:month=MAR;break;
case 4:month=APR;break;
case 5:month=MAY;break;
case 6:month=JUN;break;
case 7:month=JUL;break;
case 8:month=AUG;break;
case 9:month=SEP;break;
case 10:month=OCT;break;
case 11:month=NOV;break;
case 12:month=DEC;break;
}
/* Generating century code */
int cent=1;
int add=1,j=0;
int cent_code=century[j];
while(cent!=year)
{
cent=cent+add;
//printf("%d",cent);
if(!(cent%100))
{
if(j>3)
j=0;
cent_code=century[j++];
}
}
/* Calculation=DAY+Last Two digits of YEAR+YEAR/4+MONTHCODE+CENTURYCODE
CALCULATION/7.The remainder will be the result*/
int calc;
calc=day+two+(two/4)+month+cent_code;
calc=calc%7;
switch(calc)
{
case 0: printf("\n Its Sunday");break;
case 1: printf("\n Its Monday");break;
case 2: printf("\n Its Tuesday");break;
case 3: printf("\n Its Wednesday");break;
case 4: printf("\n Its Thursday"); break;
case 5: printf("\n Its Friday"); break;
case 6: printf("\n Its Saturday"); break;
}
return 0;
}
Comments
Post a Comment