Question

Creat a simple calendar programming that can print the month of any year Please C language...

Creat a simple calendar programming that can print the month of any year
Please C language C++ thank you
0 0
Add a comment Improve this question Transcribed image text
Answer #1

c program for printing the month of any year:-

code :-

//header files declaration
#include <stdio.h>
#include <stdlib.h>

//function declarations
int checkLeapYear( int year );      
long days( int y, int m, int d);
int leapYears( int year );       
int todayOf( int y, int m, int d);
void calendar(int y, int m);     


//main function
int main(int argc, char* argv[]){
    int year,month;
    char choice;
    while(1) {
      printf("1. Print calendar of a month\n");
      printf("2. Exit\n");
      printf("Enter your choice: ");
      scanf("\n%c", &choice);
      switch(choice)
      {
        case '1':
           printf("Enter the month and year: ");
           scanf("%d %d", &month, &year);
           calendar(year, month);
           break;
       case '2':
           printf("Bye!!");
           exit(0);
           break;
        default:
           printf("Not a valid option\n");
           break;
      }
    }
    return 0;
}

// to check year is leap or not?
int checkLeapYear( int z){
    return(z % 400 == 0) || ((z % 4 == 0) && (z % 100 != 0));
}
//count how many leap years
int leapYears( int y ){
    return y/4 - y/100 + y/400;
}

int todayOf( int y, int m, int d) {
    static int DayOfMonth[] =
        { -1,0,31,59,90,120,151,181,212,243,273,304,334};
    return DayOfMonth[m] + d + ((m>2 && checkLeapYear(y))? 1 : 0);
}

long days( int y, int m, int d){
    int lastYear;
    lastYear = y - 1;
    return 365L * lastYear + leapYears(lastYear) + todayOf(y,m,d);
}


// print calender format for the month
void calendar(int y, int m){
    int len=0, j, flag1 = 0;
    const char *NameOfMonth[] = { NULL/*dummp*/,
        "January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December"
    };
    char dayWeeks[]= "Su   Mo   Tu   We   Th   Fr   Sa";
    int DayOfMonth[] =
        { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
    int weekOfTopDay;
    int i,day;

    weekOfTopDay = days(y, m, 1) % 7;
    if(checkLeapYear(y))
        DayOfMonth[2] = 29;
    printf("\n    %s %d\n%s\n", NameOfMonth[m], y, dayWeeks);

    for(i=0;i<weekOfTopDay;i++)
        printf("     ");
    for(i=weekOfTopDay,day=1;day <= DayOfMonth[m];i++,day++){
        flag1 = 0;
        for (j = 0; j < len; j++) {
            printf("|%2d| ",day);
            flag1 = 1;
          
        }
        if (flag1 == 0) {
          printf("%2d   ",day);
        }
        if(i % 7 == 6)
            printf("\n");
    }
    printf("\n");
}


output:-

1. Print calendar of a month 2. Exit Enter your choice: 1 Enter the month and year: 05 2019 May 2019 Su Mo TuWe Th Fr Sa 4 2

C++ code:-

#include <iostream>
#include <cstdlib>
#include <iomanip>

using namespace std;

bool isLeapYear (int); //check the year is leap or not
int firstDayofFirstMonth (int );
int noofDaysInMonth (int, bool); // takes the number of the month, a flag saying whether the year is leap
void UpdateMonth (int); // takes the number of the month, and the first day, prints, and updates
void printMonth (int, int&); // the first day of the next month
void skip (int);// prints the specified amount of spaces
void skipToDay (int);// prints spaces in monthly calendar
void exitFunction (); // terminates program in case of Errors

int main ()
{
    int year, firstDayInPresentMonth;
    int currentMonth = 1;
    int numDays;
    bool leap;
    cout << "What year do you want a calendar for? ";
    cin >>year;
    cout<<endl;
    firstDayInPresentMonth=firstDayofFirstMonth(year);
    leap = isLeapYear(year);
    skip(9);
    cout << year << endl;
    while (currentMonth <= 12)
    {
    numDays = noofDaysInMonth(currentMonth, leap);
    UpdateMonth(currentMonth);
    printMonth(numDays, firstDayInPresentMonth);
    cout << endl << endl << endl;
    currentMonth = currentMonth + 1;
    }
    cout << endl;
}
bool isLeapYear (int year)
{
    return   ((year%4==0) && (year%100 !=0))||(year%400==0) ;     
}
int firstDayofFirstMonth(int year)
{
    int day_start;
    int x1, x2, x3;
    x1 = (year - 1)/ 4;
    x2 = (year - 1)/ 100;
    x3 = (year - 1)/ 400;
    day_start = (year + x1 - x2 + x3) %7;
    return day_start;
}
int noofDaysInMonth (int m, bool leap)
{
    if (m == 1) return(31);
    else if (m == 2) if (leap) return(29);else return(28);
    else if (m == 3) return(31);
    else if (m == 4) return(30);
    else if (m == 5) return(31);
    else if (m == 6) return(30);
    else if (m == 7) return(31);
    else if (m == 8) return(31);
    else if (m == 9) return(30);
    else if (m == 10) return(31);
    else if (m == 11) return(30);
    else if (m == 12) return(31);
    else exitFunction();
    return 0;
}
void UpdateMonth (int m)
{
    if (m == 1)
    {
    skip(7);
    cout << "January" << endl;
    }
    else if (m == 2) { skip(7); cout << "February" << endl; }
    else if (m == 3) { skip(7); cout << "March" << endl; }
    else if (m == 4) { skip(7); cout << "April" << endl; }
    else if (m == 5) { skip(7); cout << "May" << endl; }
    else if (m == 6) { skip(7); cout << "June" << endl; }
    else if (m == 7) { skip(7); cout << "July" << endl; }
    else if (m == 8) { skip(7); cout << "August" << endl; }
    else if (m == 9) { skip(7); cout << "September" << endl; }
    else if (m == 10) { skip(7); cout << "October" << endl; }
    else if (m == 11) { skip(7); cout << "November" << endl; }
    else if (m == 12) { skip(7); cout << "December" << endl; }
    else exitFunction();

    cout << " S M T W T F S" <<endl;
    cout << "____________________" << endl;
}
void skip (int i)
{
    while (i > 0)
    {
    cout << " ";
    i = i - 1;
    }
}
void printMonth (int numDays, int &weekDay)
{
    int day = 1;
    skipToDay(weekDay);
    while (day <= numDays)
    {
    cout << setw(2) << day << " ";
    if (weekDay == 6)
    {
        cout << endl;
        weekDay = 0;
    }
    else weekDay = weekDay + 1;
    day = day + 1;
    }
}
void skipToDay (int d)
{
    return skip(3*d);
}
void exitFunction ()
{
    cout << "exitFunction! Exiting ..." << endl;
    exit ( -1);
}

output:-

print all months in a given input year

What year do you want a calendar for? 2019 2019 January S M TWTF S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2

Add a comment
Know the answer?
Add Answer to:
Creat a simple calendar programming that can print the month of any year Please C language...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT