0% found this document useful (0 votes)
5 views4 pages

C++ Calendar Program Example

its c++ problem with solution it will help u to achieve u'r problem solving skills

Uploaded by

y.yahiaoui.inf
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

C++ Calendar Program Example

its c++ problem with solution it will help u to achieve u'r problem solving skills

Uploaded by

y.yahiaoui.inf
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Problem # 8/4 Solution Using C++

#include <iostream>
using namespace std;

bool isLeapYear(short Year)


{
// if year is divisible by 4 AND not divisible by 100
// OR if year is divisible by 400
// then it is a leap year
return (Year % 4 == 0 && Year % 100 != 0) || (Year % 400 == 0);
}

short DayOfWeekOrder(short Day, short Month, short Year)


{
short a, y, m;
a = (14 - Month) / 12;
y = Year - a;
m = Month + (12 * a) - 2;
// Gregorian:
//0:sun, 1:Mon, 2:Tue...etc
return (Day + y + (y / 4) - (y / 100) + (y / 400) + ((31 * m)
/ 12)) % 7;
}

string DayShortName(short DayOfWeekOrder)


{
string arrDayNames[] = {
"Sun","Mon","Tue","Wed","Thu","Fri","Sat" };

return arrDayNames[DayOfWeekOrder];
}

short NumberOfDaysInAMonth(short Month, short Year)


{

if (Month < 1 || Month>12)


return 0;

int days[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };


return (Month == 2) ? (isLeapYear(Year) ? 29 : 28) :
days[Month - 1];

[Link]
© Copyright 2022
Problem # 8/4 Solution Using C++

string MonthShortName(short MonthNumber)


{
string Months[12] = { "Jan", "Feb", "Mar",
"Apr", "May", "Jun",
"Jul", "Aug", "Sep",
"Oct", "Nov", "Dec"
};

return (Months[MonthNumber - 1]);


}

void PrintMonthCalendar(short Month, short Year)


{
int NumberOfDays;

// Index of the day from 0 to 6


int current = DayOfWeekOrder(1, Month, Year);

NumberOfDays = NumberOfDaysInAMonth(Month, Year);

// Print the current month name


printf("\n _______________%s_______________\n\n",
MonthShortName(Month).c_str());

// Print the columns


printf(" Sun Mon Tue Wed Thu Fri Sat\n");

// Print appropriate spaces


int i;
for (i = 0; i < current; i++)
printf(" ");

for (int j = 1; j <= NumberOfDays; j++)


{
printf("%5d", j);

if (++i == 7)
{
i = 0;
printf("\n");
}
}

printf("\n _________________________________\n");

[Link]
© Copyright 2022
Problem # 8/4 Solution Using C++

short ReadMonth()
{
short Month;
cout << "\nPlease enter a Month? ";
cin >> Month;
return Month;
}

short ReadYear()
{
short Year;
cout << "\nPlease enter a year? ";
cin >> Year;
return Year;
}

int main()

{
short Year = ReadYear();
short Month = ReadMonth();
PrintMonthCalendar(Month, Year);

system("pause>0");
return 0;
}

[Link]
© Copyright 2022

You might also like