adsense

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

Display month of any given year using c and c++

Display month of any given year using c and c++  


//Programming.OM
#include<iostream>
#include <stdlib.h>
#include<stdio.h>
#include<dos.h>
#include<conio.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)
   {
    char option;
    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};
        system("cls");
        cout<<"\n\t         "<<year;
        cout<<"\n\t ***** "<<month[0]<<" *****"<<endl;
        printMon(dayCode(1,1,year),monthCode[0]);
        for(int m=0;m<12;){
          cout<<"\n   press ";
          cout<<"\n       <n> for next \n       <p> to previous \n       <e> for exit.\n   Enter your choice : ";
          cin>>option;
          switch(option)
           {
                case 'p':
                case 'P':
                        if(m==0){
                            year--;
                            m=11;
                        }
                        else
                          m--;
                    break;
                case 'n':
                case 'N':
                        if(m==11){
                            year++;
                            m=0;
                        }
                        else
                            m++;
                    break;
                case 'e':
                case 'E':
                        exit(0);
                default:
                        cout<<"Invalid input...";


           }
        system("cls");
        cout<<"\n\t         "<<year;
        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

  •  

Post a Comment

0 Comments