C Program to find day of week given date

后端 未结 14 1392
梦谈多话
梦谈多话 2020-11-29 05:14

Is there a way to find out day of the week given date in just one line of C code?

For example

Given 19-05-2011(dd-mm-yyyy) gives me Thursday

14条回答
  •  被撕碎了的回忆
    2020-11-29 05:18

    /*
    Program to calculate the day on a given date by User
    */
    
    #include
    #include
    #include
    void main()
    {
    int dd=0,mm=0,i=0,yy=0,odd1=0,todd=0;//variable declaration for inputing the date
    int remyr=0,remyr1=0,lyrs=0,oyrs=0,cyr=0,upyr=0,leap=0;//variable declaration for calculation of odd days
    int montharr[12]={31,28,31,30,31,30,31,31,30,31,30,31};//array of month days
    clrscr();
    printf("Enter the date as DD-MM-YY for which you want to know the day\t:");
    scanf("%d%d%d",&dd,&mm,&yy);  //input the date
    /*
    check out correct date or not?
    */
    if(yy%100==0)
        {
        if(yy%400==0)
            {
            //its the leap year
            leap=1;
            if(dd>29&&mm==2)
                {
    
                printf("You have entered wrong date");
                getch();
                exit(0);
                }
            }
        else if(dd>28&&mm==2)
            {
            //not the leap year
            printf("You have entered wrong date");
            getch();
            exit(0);
    
            }
        }
    else if(yy%4==0)
        {
        //again leap year
        leap=1;
        if(dd>29&mm==2)
            {
            printf("You have entered wrong date");
            getch();
            exit(0);
    
            }
        }
    else if(dd>28&&mm==2)
        {
        //not the leap year
        printf("You have entered wrong date");
        getch();
        exit(0);
    
        }
    //if the leap year feb month contains 29 days
    if(leap==1)
    {
    montharr[1]=29;
    
    }
    //check date,month,year should not be beyond the limits
    
    if((mm>12)||(dd>31)|| (yy>5000))
        {
        printf("Your date is wrong");
        getch();
        exit(0);
    
        }
    //odd months should not contain more than 31 days
    if((dd>31 && (mm == 1||mm==3||mm==5||mm==7||mm==8||mm==10||mm==12)))
        {
        printf("Your date is wrong");
        getch();
        exit(0);
    
        }
    //even months should not contains more than 30 days
    
    if((dd>30 && (mm == 4||mm==6||mm==9||mm==11)))
        {
        printf("Your date is wrong");
        getch();
        exit(0);
    
        }
    
    //logic to calculate odd days.....
    printf("\nYou have entered date: %d-%d-%d ",dd,mm,yy);
    remyr1=yy-1;
    remyr=remyr1%400;
    
    cyr=remyr/100;
    if(remyr==0)
        {
        oyrs=0;
        }
    else if(cyr==0 && remyr>0)
        {
        oyrs=0;
        }
    
    
    else if(cyr==1)
        {
        oyrs=5;
        }
    else if(cyr==2)
        {
        oyrs=3;
        }
    else if(cyr==3)
        {
        oyrs=1;
        }
    
        upyr=remyr%100;
        lyrs=upyr/4;
        odd1=lyrs+upyr;
        odd1=odd1%7;
        odd1=odd1+oyrs;
        for(i=0;i7)
            todd=todd%7;    //total odd days gives the re quired day....
        printf("\n\nThe day on %d-%d-%d :",dd,mm,yy);
    
        if(todd==0)
            printf("Sunday");
        if(todd==1)
            printf("Monday");
        if(todd==2)
            printf("Tuesday");
        if(todd==3)
            printf("Wednesday");
        if(todd==4)
            printf("Thrusday");
        if(todd==5)
            printf("Friday");
        if(todd==6)
            printf("Saturday");
    getch();
    }
    

提交回复
热议问题