Program in C and C++ to display day of any given date.
C code
#include<stdio.h> int c_Code(int c_year) { if(c_year%4==0&&c_year%100!=0||c_year%400==0) return 0; else if(c_year==1700||c_year==2100||c_year==2500||c_year==2900) return 5; else if(c_year==1800||c_year==2200||c_year==2600||c_year==3000) return 3; else if(c_year==1900||c_year==2300||c_year==2700||c_year==3100) return 1; else return 0; } int main() { char dayPrint[8][15]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; int i,cy,cc,yc,yl,day,days=0,month,year,dayCode; printf("Enter day, month, year : "); scanf("%d%d%d",&day,&month,&year); int monthCode[]={31,year%4==0&&year%100!=0||year%400==0?29:28,31,30,31,30,31,31,30,31,30,31}; yc=(year-1)%100; cy=(year-1)-yc; yl=yc/4; cc=c_Code(cy); for(i=0;i<month-1;i++){ days+=monthCode[i]; } days=days+day; dayCode=(cc+yc%7+yl%7+days%7)%7; printf("Day : %s",dayPrint[dayCode]); return 0; }
C++ code
#include<iostream> using namespace std; int c_Code(int c_year) { if(c_year%4==0&&c_year%100!=0||c_year%400==0) return 0; else if(c_year==1700||c_year==2100||c_year==2500||c_year==2900) return 5; else if(c_year==1800||c_year==2200||c_year==2600||c_year==3000) return 3; else if(c_year==1900||c_year==2300||c_year==2700||c_year==3100) return 1; else return 0; } int main() { char dayPrint[8][15]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; int i,cy,cc,yc,yl,day,days=0,month,year,dayCode; cout<<"Enter day, month, year : "; cin>>day>>month>>year; int monthCode[]={31,year%4==0&&year%100!=0||year%400==0?29:28,31,30,31,30,31,31,30,31,30,31}; yc=(year-1)%100; cy=(year-1)-yc; yl=yc/4; cc=c_Code(cy); for(i=0;i<month-1;i++){ days+=monthCode[i]; } days=days+day; dayCode=(cc+yc%7+yl%7+days%7)%7; cout<<"Day : "<<dayPrint[dayCode]; return 0; }
Output :-
Related Programs
0 Comments