adsense

Hii, welcome to my site. My name is Om prakash kartik. This blog helps you to learn programming languages concepts.

Print calendar for a given year in C and C++

Print calendar for a given year in C and C++

#include<iostream>
#include<stdio.h>
 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)
         return 5;
       else if(c_year==1800||c_year==2200)
        return 3;
       else if(c_year==1900||c_year==2300)
        return 1;
   }
 int dayCode(int day,int month,int year){
     int i,c_year,ccode,ycode,lyear,days=0,dayCode;
       int monthCode[]={31,year%4==0&&year%100!=0||year%400==0?29:28,31,30,31,30,31,31,30,31,30,31};
       ycode=(year-1)%100;
       c_year=(year-1)-ycode;
       lyear=ycode/4;
       ccode=c_Code(c_year);
       for(i=0;i<month-1;i++){
         days+=monthCode[i];
       }
       days=days+day;
       dayCode=(ccode+ycode%7+lyear%7+days%7)%7;
       return dayCode;
 }
  void printMon(int dayc,int mon){
    int i,j,k=1;
    printf("   ---------------------------------\n");
    printf("   Sun\tMon  Tue  Wed  Thu  Fri  Sat\n");
    printf("   ---------------------------------\n");
      for(i=1;i<=6;i++){
        for(j=0;j<7&&k<=mon;j++)
        {
            if(i==1&&j<dayc)
               printf("     ");
            else
      printf("%5d",k++);
         }
        cout<<endl;
      }
      printf("   ---------------------------------");
  }
  void print_calendar(int year)
   {
    if(year>0){
       char month[][20]={"JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER"};      
    int monthCode[]={31,year%4==0&&year%100!=0||year%400==0?29:28,31,30,31,30,31,31,30,31,30,31};
       for(int m=0;m<12;m++){
           cout<<"\n\t ***** "<<month[m]<<" *****"<<endl;
           printMon(dayCode(1,m+1,year),monthCode[m]);
        }
      }
     else
        cout<<"Invalid year...";
   }
 int main()
  {
      int year;
      cout<<"Enter year : ";
      cin>>year;
      print_calendar(year);
      return 0;
  }
Output :-
 























Related programs 
 Coming soon:

  •       Program in C and C++ to print day of a given date.

Post a Comment

1 Comments