Find Day From Day In C Without Using Function

A complete year consists of 12 months, each varying in duration:

February 28 (in a non-leap year) and March 29 (in a Leap Year)

March 31 to April 30, May 31 to June 30, August 31 to September 30, and December 31

The overall sum amounts to 365 for a Non-Leap Year and 366 for a Leap Year.

We uncovered a fact dated back to 01-01-0001, which corresponds to Monday, January 1st, 0001, marking the program's inception. Furthermore, we utilize a variable called day value, which represents Monday, Tuesday, Thursday, Friday, Saturday, and Sunday as 0, 1, 2, 3, 4, and 6, respectively.

Example

# include < stdio . h > 
# include < conio . h > 
void main ( ) 
{ 
 int month [ 12 ] = { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } ;  
 char week [ 7 ] [ 10 ] ;
 int date , mon , year , i , r , s = 0 ; 
 clrscr ( ) ; 
 strcpy ( week [ 0 ] , " Sunday " ) ; 
 strcpy ( week [ 1 ] , " Monday " ) ; 
 strcpy ( week [ 2 ] , " Tuesday " ) ; 
 strcpy ( week [ 3 ] , " Wednesday " ) ; 
 strcpy ( week [ 4 ] , " Thursday " ) ; 
 strcpy ( week [ 5 ] , " Friday " ) ; 
 strcpy ( week [ 6 ] , " Saturday " ) ; 
 printf ( " ?nter a valid date ( dd / mm / yyyy ) : " ) ; 
 scanf ( " % d / % d / % d " , & date , & mon , & year ) ; 
 if ( ( year % 400 = = 0 ) | | ( ( year % 4 = = 0 ) & & ( year % 100 ! = 0 ) ) ) 
  month [ 1 ] = 29 ; 
 for ( i = 0 ; i < mon - 1 ; i + + ) 
 s = s + month [ i ] ; 
 s = s + ( date + year + ( year / 4 ) - 2 ) ; 
 s = s % 7 ; 
 printf ( " \ n The day is : % s " , week [ s ] ) ; 
 getch ( ) ; 
}

Output:

Output

?nter a valid date ( dd / mm / yyyy ) : 05/07/2022

The day is : Tuesday
..................................................
Process executed in 1.22 seconds
Press any key to continue.

Explanation

A set of 12 unique integers can be stored in the integer array month[12], which is preassigned during declaration. The values in this array correspond to the number of days in each respective month; for instance, the initial value of 31 indicates the length of January. Char7 refers to a separate multidimensional character array distinct from month[12], capable of accommodating up to seven entries, each between nine and ten characters long. This array represents the days of the week, such as Sunday and Monday. These integers are utilized to capture user-provided date information, along with intermediary variables like I, r, and s. In this context, the strcpy function from the C programming language is employed to copy strings, differing from the initialization method used for the char7 array as seen with the month[12] array.

The strcpy function is employed to set the initial values of week7. Subsequently, we prompt the user for preformatted input. By structuring the scanf function to separate each integer with a "/", we ensure that all three values from the user input in the format dd/mm/yyyy are promptly stored in their designated variables.

The conditional statement is used to check if the user-provided year is a leap year. In the case of a leap year, the number of days in February is adjusted to 29 by setting month [1] = 29.

Input Required

This code uses input(). Please provide values below: