In this tutorial you will learn about the C Program to Display Day of the month and its application with practical example.
In this tutorial, we will learn to create a C program that will accept year,month and date from user and display the day of month using C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- C Operators.
- Basic input/output.
- C loop statements
- C switch case.
- Function in C.
Example:
If you give value input in particular format like year=2021, Month=5, and day is 12 then this program will display the output as The Given Date is : 12/05/2021 and Day of Week of the Date : Wednesday.
C Program to Display Day of the month.
In this program we will print name of the Day of week and Date of calendar for given year, month and day. Please check out the program below to print, Name of Day of week and Date of calendar.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
#include<stdio.h> #include<math.h> int main(){ // taking year,month and day from user... int date, month, year; printf("Enter year: "); scanf("%d",&year); printf("Enter month : "); scanf("%d",&month); printf("Enter date : "); scanf("%d",&date); week_day(date,month,year); // passing to function.. return 0; } int week_day(int date, int month, int year) { int weekday, yy,century ; yy = year % 100; century = year / 100; printf("The Date Given is : %d / %d / %d \n\n", date, month, year); weekday = 1.25 * yy + findmonth(month, year) + date - 2 * (century % 4); weekday = weekday % 7; switch (weekday){ // finding the day of given date,month. case 0: printf("Day of Week is : Saturday"); break; case 1: printf("Day of Week is : Sunday"); break; case 2: printf("Day of Week is : Monday"); break; case 3: printf("Day of Week is : Tuesday"); break; case 4: printf("Day of Week is : Wednesday"); break; case 5: printf("Day of Week is : Thursday"); break; case 6: printf("Day of Week is : Friday"); break; default: printf("The inputed data is wrong"); } return 0; } int findmonth(int months, int year){ int find_month, leapyear; if ((year % 100 == 0) && (year % 400 != 0)) leapyear = 0; else if (year % 4 == 0) leapyear = 1; else leapyear = 0; find_month = 3 + (2 - leapyear) * ((months + 2) / (2 * months)) + (5 * months + months / 9) / 2; find_month = find_month % 7; return find_month; } |
Output
In the above program, we have first declared and initialized a set variables required in the program.
This program will accept three values from user Year,Month and Date.Then we passes all three value to function called week_day it will accept all three values.With the help or week_day() function we calculate month day and week day.Within this function we gain called a function findmonth(). we find the month value and returns the value to week_day() function where we will find the day of week and print our required output.