gargaryan / practice-prog

codes for practice...
1 stars 0 forks source link

to find the day of given date ,month, year #1

Open gargaryan opened 5 years ago

gargaryan commented 5 years ago

include

int main() { int year,date,month,n,m,l,o,k,p,q; printf("\t\t\t \tenter \ndate\nmonth\nyear\n"); scanf("%d%d%d",&date,&month,&year); n=year%100; //Extracing last two digit of year m=n+date; //add date in last two digit number of year if(month==1||month==10) {

   l=m+1;               //add month code
}
    else if(month==2||month==3||month==11)

           {
            l=m+4;

           }
           else if(month==4||month==7)
           {
                l=m+0;

           }
           else if(month==5)
           {
              l=m+2;

           }
           else if(month==6)
                {
                    l=m+5;
                }
                else if(month==8)
                    {
                        l=m+3;

                    }
                    else if(month==9||month==12)
                    {
                        l=m+6;
                    }
                          if(n%4==0)     //code for checking leap year
                          {
                            if(month==1||month==2)  //if date is in january or february one is to be subtracted
                            {
                                o=l-1;
                            }
                            else
                           {
                                o=l-0;
                           }

                          }
        if(year>=1700&&year<1800)       // add century code
        {
                k=o+4;
        }
        else if(year>=1800&&year<1900)
        {
                k=o+2;
        }
        else if(year>=1900&&year<2000)
        {
                k=o+0;
        }
        else if(year>=1900&&year<3000)
        {
                k=o+6;

        }
        p=k+n;           //add last digit of year with k
         q=p%7;          //weak code will come

        if(q==0)
        {
         printf("sunday");             //week code
        }
        else if(q==1)
        {
          printf("monday");
        }
        else if(q==2)
        {
          printf("tuesday");
        }
        else if(q==3)
        {
            printf("wednesday");
        }
        else if(q==4)
        {
            printf("thrusday");
        }
        else if(q==5)
        {
            printf("friday");
        }
        else if(q==6)
        {
            printf("saturday");
        }

return 0;

}

dbc2201 commented 5 years ago

Hi @gargaryan, the "logic" of the program seems to be off. I've modified your source code here

dbc2201 commented 5 years ago
/*
 * Filename : year.c
 * Author : Aryan Garg, B. Tech 1st Year, GLAU
 * Modifier : Divyansh Bhardwaj, Technical Trainer, GLAU
 * Aim : To find the day of the week on a given date.
 * */
#include<stdio.h>
int main()
{
    int year = 0, date = 0, month = 0, last_two_digits = 0, temp_m = 0, temp_l = 0,
    temp_o = 0, century_code = 0, temp_p = 0, week_code = 0;

    printf("Enter the date: ");
    scanf("%d", &date);

    printf("Enter the month (1 for Jan, 2 for Feb and so on...): ");
    scanf("%d", &month);

    printf("Enter the year: ");
    scanf("%d", &year);

    last_two_digits = year % 100; //Extracing last two digit of year
    temp_m = last_two_digits + date; //add date in last two digit number of year

    switch (month)
    {
        case 1  :
        case 10 :   temp_l = temp_m + 1;
                    break;
        case 2  :
        case 3  :
        case 11 :   temp_l = temp_m + 4;
                    break;
        case 4  :
        case 7  :   temp_l = temp_m + 0;
                    break;
        case 5  :   temp_l = temp_m + 2;
                    break;
        case 6  :   temp_l = temp_m + 5;
                    break;
        case 8  :   temp_l = temp_m + 3;
                    break;
        case 9  :
        case 12 :   temp_l = temp_m + 16;
                    break;

        default:    printf("Error 1");  //  error 1 means switch case failed,
                                        // value of month is invalid or zero
    }

    if (last_two_digits % 4 == 0)     //code for checking leap year
    {
        if (month == 1 || month == 2)  //if date is in january or
                                       // february one is to be subtracted
        {
            temp_o = temp_l - 1;
        }
        else
        {
            temp_o = temp_l - 0;
        }

    }
    if ( year >= 1700 && year < 1800 )       // add century code
    {
        century_code = temp_o + 4;
    }
    else if ( year >= 1800 && year < 1900 )
    {
        century_code = temp_o + 2;
    }
    else if ( year >= 1900 && year < 2000 )
    {
        century_code = temp_o + 0;
    }
    else if( year >= 1900 && year < 3000 )
    {
        century_code = temp_o + 6;
    }
    temp_p = century_code + last_two_digits;           //add last digit of year with century_code
    week_code = temp_p % 7;  //week code will come
    switch (week_code)
    {
        case 0  :   printf("Sunday");
            break;
        case 1  :   printf("Monday");
            break;
        case 2  :   printf("Tuesday");
            break;
        case 3  :   printf("Wednesday");
            break;
        case 4  :   printf("Thursday");
            break;
        case 5  :   printf("Friday");
            break;
        case 6  :   printf("Saturday");
            break;
        case 7  :   printf("Sunday");
            break;
        default: printf("Error 2"); // Error 2 means the final switch failed
                                    // value of week_code is invalid or zero
    }
    return 0;

}