C++ program example: calendar


Print calendar

This is the C++ program example to print a calendar of a specific year. The program asks the user to input the year for which the calendar would be printed.

#include<iostream>
#include<conio.h>
#include<iomanip>

using namespace std;

void printPascalTr(int size);
int getFirstDay(int year,int month);
int getDayCode(int year);
void printCar(int year,int month);
int main()

{

/* Carlenda writen by:

*/

int i, year;
char ch='y';
while(ch=='y'){
cout<<"Enter year of the carlenda:";//year input
cin>>year;
//print carlenda for the input year
for(i=1;i<=12;i++)
printCar(year,i);
cout<<"Continue? press y:";
cin>>ch;

}

getch();
return 0;

}

// calculate the first day of week for current month
int getFirstDay(int year,int month){
int fday=getDayCode(year);
int num_days=0; // number of days of the year before the current month
int dCode=getDayCode(year);
switch(month){
case 12: num_days+=30;
case 11: num_days+=31;
case 10: num_days+=30;
case 9: num_days+=31;
case 8: num_days+=31;
case 7: num_days+=30;
case 6: num_days+=31;
case 5: num_days+=30;
case 4: num_days+=31;
case 3: num_days+=28;
case 2: num_days+=31;

}

if(month==1) return(fday);else if((year%4==0) || (year%400==0) && month>2) return (num_days+1+dCode)%7 ;
// leap year

else return (num_days+dCode)%7;}

// calculate the first day of the year
int getDayCode(int year){
int fday;
fday=(((year-1)*365+(year-1)/4-(year-1)/100+(year-1)/400)+1)%7;

return fday;

}


void printCar(int year, int month){
int fday_month=getFirstDay(year,month);
int code[7]={0,1,2,3,4,5,6};
char *Day[7]={"Sun","Mon","Tues","Wed","Thu","Fri","Sat"};
char *Month[12]={"January","February","March","April","May","June","Junly","August","September","October","November","December"};

int num_days,i;

if(month==1||month==3||month==5||month==7|| month==8|| month==10|| month==12)
num_days=31;
else if(month==2)
{

; if((year%4==0) || (year%400==0)) num_days=29;else num_days=28;

}

else num_days=30;
cout<<"\n.............."<
cout<<"First day of the month:"<
cout<<"Sun\t"<<"Mon\t"<<"Tues\t"<<"Wed\t"<<"Thu\t"<<"Fri\t"<<"Sat"<
for(i=1;i<=fday_month;i++) cout<<"\t";
int d=1;
int con=0;
while(d<=num_days)
{
cout<<d<<"\t";
if(fday_month>0) {
if(d==7-fday_month) {cout<<"\n";con=d;}
else if(d%(con+7)==0) {cout<<"\n";con=d;}

}

else if(d%7==0) cout<<"\n";

d++;

}

cout<<"\n";
}




Comments

Afzal Khan comment

 Afzal Khan

#shivi
the link given below asks for year and then produces the calendar for that year ..., a complete calendar


2017-01-29
Afzal Khan comment

 Afzal Khan

Solution to your request :
http://code.runnable.com/VOUBTOX4fTgXhF8o/calendar-program-for-c%2B%2B
try it and comment...


2017-01-29
Saichie comment

 Saichie

How can you do the code when you input a negative sign and the output will be INVALID?


2016-09-20
shivi comment

 shivi

sirf mere ek date month n year enter krne se pura calendar display ho to wo code ka link snd kro muje


2016-08-29
Anonymous comment

 Anonymous

There is an error on line 93 please fix


2015-07-08
adnan comment

 adnan

yr yeh to sirf ek din add krta ha ha or rotate bhi nhai hota iss ka month or year agr koi complete program ha to send karo ya upload karo plz its urgent....


2014-10-16



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.