[c] Calendar
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- #include <stdio.h>
- #include <stdlib.h>
- // Function to check if a given year is a leap year
- int isLeapYear(int year) {
- return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
- }
- // Function to get the number of days in a given month
- int daysInMonth(int month, int year) {
- int days[] = {31, 28 + isLeapYear(year), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- return days[month - 1];
- }
- // Function to get the day of the week for a given date using Zeller's Congruence algorithm
- int getDayOfWeek(int day, int month, int year) {
- if (month < 3) {
- month += 12;
- year--;
- }
- int K = year % 100;
- int J = year / 100;
- int h = (day + 13 * (month + 1) / 5 + K + K / 4 + J / 4 + 5 * J) % 7;
- return (h + 5) % 7; // 0 for Saturday, 1 for Sunday, ..., 6 for Friday
- }
- // Function to count the number of Fridays in a given year
- int countFridays(int year) {
- int count = 0;
- for (int month = 1; month <= 12; month++) {
- for (int day = 1; day <= daysInMonth(month, year); day++) {
- if (getDayOfWeek(day, month, year) == 6) { // Friday is represented as 6
- count++;
- }
- }
- }
- return count;
- }
- // Function to print the calendar for a given year
- void printCalendar(int year) {
- printf("Calendar for %d:\n", year);
- for (int month = 1; month <= 12; month++) {
- printf("\e[0;32m");
- printf("\n\n\t%s\n\n", (month == 1) ? "January" : (month == 2) ? "February" : (month == 3) ? "March" : (month == 4) ? "April" : (month == 5) ? "May" : (month == 6) ? "June" : (month == 7) ? "July" : (month == 8) ? "August" : (month == 9) ? "September" : (month == 10) ? "October" : (month == 11) ? "November" : "December");
- printf("\033[0m"); // Reset color
- printf("Sun Mon Tue Wed Thu Fri Sat\n");
- int startingDay = getDayOfWeek(1, month, year);
- for (int i = 0; i < startingDay; i++) {
- printf(" ");
- }
- for (int day = 1; day <= daysInMonth(month, year); day++) {
- if(startingDay == 5 || startingDay == 6) {
- printf("\e[0;31m"); // print red color on Friday and Saturday to mark holiday
- }
- printf("%3d ", day);
- printf("\033[0m"); // Reset color
- startingDay++;
- if (startingDay > 6) {
- printf("\n");
- startingDay = 0;
- }
- }
- printf("\n");
- }
- }
- // Main function
- int main() {
- int year;
- printf("Enter the year: ");
- scanf("%d", &year);
- printCalendar(year);
- int fridays = countFridays(year);
- printf("\nNumber of Fridays in %d: %d\n", year, fridays);
- getch();
- return 0;
- }
Editor
You can edit this paste and save as new: