I'm from Kolkata (India) & a student of Computer Science Hons., I started to learn C from August, Really enjoying it. College recently gave some assignments to us, I've completed all of them. So I thought to share a program with you guys that excited me a lot. I often stumble upon this site while searching about various things in Google. I'm not even a noob in front you guys making money online by your programming skill. So, please take a look at the code if you dont mind. The program tells you to enter a date & outputs the day : #include<stdio.h> int lyrange(int,int); int lipyear(int); int month(int,int); int mod(int); int day(int,int); main() { int d1,m1,y1,d2,m2,y2,total=0,i,d=4,m=12,y=1988,di,mi,yi,r,f; printf("\nEnter a date:"); printf("\nDate: "); scanf("%d",&di); printf("\nMonth: "); scanf("%d",&mi); printf("\nYear: "); scanf("%d",&yi); if(yi>y || (yi==y && (mi>m || (mi==m && di>d)))) { d1=d; m1=m; y1=y; d2=di; m2=mi; y2=yi; f=1; } else if(yi<y || (yi==y && (mi<m || (mi==m && di<d)))) { d1=di; m1=mi; y1=yi; d2=d; m2=m; y2=y; f=0; } else { printf("\nSunday"); return; } if(y1!=y2) { total+=365*(y2-y1-1)+lyrange(y1+1,y2-1); for(i=m1;i<=12;i++) total+=(i==m1)?(month(i,y1)-d1):month(i,y1); for(i=1;i<=m2;i++) total+=(i==m2)?d2:month(i,y2); } else if(m1!=m2) { for(i=m1;i<m2;i++) total+=(i==m1)?(month(i,y1)-d1):month(i,y1); total+=d2; } else total+=d2-d1; r=total%7; day(r,f); } int lyrange(int x,int y) { int i,c=0,g=1; for(i=x;i<=y;i+=g) if(i%4==0 && (i%100!=0 || i%400==0)) { c++; g=4; } return c; } int lipyear(int x) { if(x%4==0 && (x%100!=0 || x%400==0)) return 1; else return 0; } int month(int m,int y) { switch(m) { case 1: return 31; case 2: if(y%4==0 && (y%100!=0 || y%400==0)) return 29; else return 28; case 3: return 31; case 4: return 30; case 5: return 31; case 6: return 30; case 7: return 31; case 8: return 31; case 9: return 30; case 10: return 31; case 11: return 30; case 12: return 31; } } int mod(int x) { return (x<0)?-x:x; } int day(int r,int y) { if(y==1) { switch(r) { case 0: printf("\nSunday\n"); break; case 1: printf("\nMonday\n"); break; case 2: printf("\nTuesday\n"); break; case 3: printf("\nWednesday\n"); break; case 4: printf("\nThursday\n"); break; case 5: printf("\nFriday\n"); break; case 6: printf("\nSaturday\n"); } } else if(y==0) { switch(r) { case 0: printf("\nSunday\n"); break; case 1: printf("\nSaturday\n"); break; case 2: printf("\nFriday\n"); break; case 3: printf("\nThursday\n"); break; case 4: printf("\nWednessday\n"); break; case 5: printf("\nTuesday\n"); break; case 6: printf("\nSunday\n"); } } } Code (markup):
Not a bad start for a cs student. I would say it needs some refactoring though. Check out the author Martin Fowler and his refactoring book. It is one of the best books in the business.
You really don't need to do all that figuring. The equation... #include "iostream" using namespace std; void getDay(int m, int d, int y, char* (&myDay)[7]); int main() { char* myDay[7] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; int m = 12; int d = 24; int y = 2007; getDay(m, d, y, myDay); return 0; } void getDay(int m, int d, int y, char* (&myDay)[7]) { d += m < 3 ? y-- : y-2; int n = (23*m/9+d+4+y/4-y/100+y/400)%7; cout << myDay[n] << '\n'; } Code (markup):
As I said, I am still a noob in C, not even completed the text book. Looks like a guy wrote some c++ code here, lol