Compute number of days between two dates of the same year. A date is given
as two numbers (day, month).
Assume that month of Feb has 28 days

We will be able to help you if you firstly show us the code you have tried so far ...

#include <stdio.h>
#include <math.h>
int main()
{
int x,y;
int days_bw_two_dates [13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
{
int a,b,c=0,d=0;
printf("type Date \n");
scanf("%d %d",&a, &b);
do
{
d=d+days_bw_two_dates[c];
c++;
}
while(c<b);
x=a+d;
}
{
int a,b,c=0,d=0;
printf("type Date  \n");
scanf("%d %d",&a, &b);
do
{
d+=days_bw_two_dates[c];
c++;
}
while(c<b);
y=a+d;
}
printf("days between both dates are %d \n", abs(x-y));
}

OK, that's your code and your problem with it is?

Without you stating the particular problem with which you wish to have help ...

here are several ways to begin to clear up your code and make it easier to see if logic and code ok:

  • Indent blocks of code.

  • Use descriptive variable names.

  • Use functions (a function for each task.)

  • Use comments and blank lines to document what is happening in each section of your code ... as you go.

  • Take in only valid input ... and do not let program crash if input was invalid.

And here is an example of the above ... to get you started:

/* daysBetween.c */ /*  2015-08-29 */

#include <stdio.h>
#include <math.h>


const int DAYS_IN_MONTH[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

/* 2 handy utilities for many C student coding problems ... */
int takeInChr( const char* msg )
{
    char chr;
    printf( msg ); fflush( stdout );
    chr = getchar();
    if( chr != '\n' ) while( getchar() != '\n' ) ; /* flush stdin ... */
    return chr;
}
int more() /* defaults to 'true'/'yes'/'1' ... unless 'n' or 'N' entered */
{
    int c = takeInChr( "More (y/n) ? " );
    if( c == 'n' || c == 'N' ) return 0;
    /* else ... */
    return 1;
}


/* returns 1 if is leap year, else returns 0 ... */
int isLeap()
{
    char reply = takeInChr( "Is this a leap year (y/n) ? " );
    if( reply == 'y' || reply == 'Y' ) return 1;
    if( reply == 'n' || reply == 'N' ) return 0;

    /* if reach here ... loop again ... */
    printf( "You must enter a letter in range yYnN! Try again ...\n" );
    return isLeap(); /* an example of tail recursive call / loop  ... */
}

/* take in month, day and return day number ... */
int dayNumber()
{
    int i, month, day, daysInMonth, numGood, numDays = 0, leap = 0;
    for(  ; ; ) /* loop forever ... until break ... */
    {
        printf( "Enter valid month number (1..12) and valid day number (1..31): " );
        fflush( stdout );

        numGood = scanf( "%d %d", &month, &day );
        if( numGood == 2 && getchar() == '\n' )
        {
            if( month < 1 || month > 12 ) /* ensure valid month was entered ... */
            {
                printf( "Try again ... valid months are 1..12\n" );
                continue; /* from top of forever loop right now ... */
            }

            if( month >= 2 )
                leap = isLeap();

            daysInMonth = DAYS_IN_MONTH[month];
            if( month == 2 && leap == 1 ) ++ daysInMonth;

            if( day < 1 || day > daysInMonth ) /* ensure valid day was entered ... */
            {
                printf( "Try again ... valid days for month %d are 1..%d\n", month, daysInMonth );
                continue; /* from top of forever loop right now ... */
            }

            /* if reach here ... then good data was entered ... so ... */
            break;
        }
        else
        {
            while( getchar() != '\n' ) ; /* flush stdin */
            printf( "Try again ... with valid entries for month and year ...\n" );
        }
    }

    /* sum up days before this month ... */
    for( i = 1; i < month; ++ i )
        numDays += DAYS_IN_MONTH[i];

    if( month < 3 )
        return numDays + day;

    return numDays + day + leap;
}




int main()
{
    do
    {
        int dayNum1 = dayNumber(),
            dayNum2 = dayNumber();

        printf( "Inclusive days between day number %d and day number %d = %d \n",
                dayNum2, dayNum1, abs(dayNum2-dayNum1) + 1 );
    }
    while( more() );

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.