Find the total number of days in C The problem in C finds the number of days between two dates

Updated on technology 2024-05-13
11 answers
  1. Anonymous users2024-02-10

    Your logic is not wrong.

    There's a slight mistake with your format: scanf("%d,%d,%d", y, m, d) should be replaced by :

    scanf("%d%d%d",&y,&m,&d);

  2. Anonymous users2024-02-09

    To calculate the number of days between two years, months and days, the idea is to calculate the total number of days on each day and subtract them.

    It is necessary to consider the situation of leap years, and judge the formula for leap years: every leap in 4 years, no leap in 100 years, and then in 400 years.

    year % 4 == 0 &&year % 100 != 0) |year % 400 == 0)

    I found one on the net (lazy = =!), modified as follows:

    #include

    int sum(int y,int m,int d);int i,s=0;

    for(i=1;i s2)

    printf("Number of days apart: %ld",s1-s2);

    elseprintf("Number of days apart: %ld",s2-s1);

    The above **VC6 compilation test passed.

    Although this idea seems a bit stupid, other algorithms, ** are too long and complicated, and it is better to directly calculate the number of days between the two dates and January 1 of the first year of the Common Era, and then subtract them.

  3. Anonymous users2024-02-08

    Hello! Your program is working fine, it's not because there is a problem with the format when you enter the data.

  4. Anonymous users2024-02-07

    [Procedure 1]:

    main()

    sum=sum+day;*Plus the number of days on a given day* to determine if it is a leap year*

    leap=1;

    elseleap=0;

    if(leap==1&&month>2) *If it is a leap year and the month is greater than 2, the total number of days should be added by one day*

    sum++;

    printf("it is the %dth day.",sum);}

  5. Anonymous users2024-02-06

    [Procedure 1]:

    main()

    sum=sum+day;*Plus the number of days on a given day* to determine if it is a leap year*

    leap=1;

    elseleap=0;

    if(leap==1&&month>2) *If it is a leap year and the month is greater than 2, the total number of days should be added by one day*

    sum++;

    printf("it is the %dth day.",sum);}

  6. Anonymous users2024-02-05

    The format is separated by commas, but "2011 3 17" is separated by spaces, so the input does not get the correct value, and the subsequent calculation results are bound to be wrong.

  7. Anonymous users2024-02-04

    I haven't used C for a long time, and I remember that the input data should be like this: 2011, 3, 17. I don't know if I'm mistaken.

  8. Anonymous users2024-02-03

    Both programs calculate the correct number of days.

    ps: There is a small mistake in the first part of the program, sum+=a[i]; and sum+=b[i]; , i should be replaced by j

    However, because the second part of the program has to judge if(leap(year)) many times, that is, it needs to execute the function int leap(int a) many times. In the first segment, the function int leap(int a) is executed only once. Therefore, the efficiency of program segment 1 is higher than program segment 2.

    Test**: include

    int leap(int year)

    main()

    int b[12]=;

    int j,sum,year,month,day;

    first paragraph * sum=0;

    if(leap(year))

    for(j=1;The number of days for the calculation of the results of the jrun is the same.

  9. Anonymous users2024-02-02

    The second one is definitely wrong, the difference between a leap year and a peaceful year is that February is 28, 29

    But the second program, which adds 1 more every month

    for(j=1;j

  10. Anonymous users2024-02-01

    The subscript of the array in the second segment of the program is incorrect.

  11. Anonymous users2024-01-31

    Put int caldays function.

    if(year1=year2) is changed to if(year1==year2).

    This is the error, and it should be possible to find it by visual inspection after correcting it.

Related questions
8 answers2024-05-13

*Find 2+4+6+8+....+100 and *

#include "" >>>More

19 answers2024-05-13

1 Don't give up, think slowly.

2 Don't forget that C++ and C can be compared to each other, the biggest difference is only in the output part. >>>More

18 answers2024-05-13

#include ""

void main() >>>More

5 answers2024-05-13

Mistake 1: Because the three-digit number you are looking for is an integer, all variables should be int and not float. >>>More

5 answers2024-05-13

int max(float fx,float fy)float fz;

fz=fx>fy?fx:fy; >>>More