Thread: Leap year program prints wrong output

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    71

    Leap year program prints wrong output

    Hi,

    This program is supposed to print all the leap year between any two given years. It seems to work when the gap between years is huge, but when I enter 1991 and 2005 for example, it will only tell me that 2000 is a leap year, when years such as 92,96, and 04 are also leap years, right?

    Any ideas where Iv'e gone wrong?

    Code:
    #include <stdio.h>
    #include <process.h>
    #include <values.h>
    
    int main(void)
    {
       char  your_name[12];
       int   year_start;
       int   year_finish;
       int   leap_yr_input = 0;
       int   leap_yr_test = 0;
       int   maxint_value;
       int   i;
       int   yr_test;
       int   nearest_leap_yr;
    
       system("cls");
    
       printf("\nPlease enter your name(e.g. Jan): ");
       scanf("%s", your_name);
    
       maxint_value = MAXINT;
    
       do
       {
          printf("\nEnter year to begin with between 1 and %d: ", maxint_value - 1);
          scanf("%d", &year_start);
    
          printf("\nEnter an ending year between %d and %d(e.g. %d): ",
                 year_start + 1, maxint_value, year_start + 1);
          scanf("%d", &year_finish);
    
          printf("\n");
    
          if(year_start >= year_finish)
          {
             printf("\nInvalid input!");
             printf("\nThe ending year must be bigger than the starting year!");
             printf("\n\nRe-enter years");
          }
       }
       while( year_start >= year_finish );
    
       printf("\nHello %s!", your_name);
       printf("\n");
       printf("\nYour starting year is: %d", year_start);
       printf("\nYour ending year is: %d", year_finish);
       printf("\n\n");
    
       for( i = year_start; i <= year_finish; i++ )
       {
          if( i % 4000 != 0 )
          {
             if( i % 4 == 0 )
             {
                if( i % 400 == 0 )
                {
                   leap_yr_input = 1;
                   printf("\nThe year %d is a leap year", i);
                }
             }
          }
       }
    
       if( leap_yr_input == 1 )
          printf("\n\nThis is the end of LEAP year list");
    
       if( leap_yr_input == 0 )
       {
          printf("\nThere are no leap years in your input range");
    
          for( yr_test = year_finish + 1; yr_test <= maxint_value; yr_test ++)
          {
             if(yr_test % 4000!= 0)
             {
                if(yr_test % 4 == 0)
                {
                   if(yr_test % 400 == 0)
                   {
                      leap_yr_test = 1;
                      nearest_leap_yr = yr_test;
                      break;
                   }
                   else if(yr_test % 100 != 0)
                   {
                      leap_yr_test = 1;
                      nearest_leap_yr = yr_test;
                      break;
                   }
                }
             }
          }
    
          if( leap_yr_test == 0)
          {
             printf("\nThere is NO leap year in the range between ");
             printf("your input and MAXINT");
          }
    
          if(leap_yr_test == 1)
          {
             printf("\n\nThe nearest leap year that is greater than your input ");
             printf("is: %d", nearest_leap_yr);
          }
       }
    
       printf("\n");
       printf("\nThanks for using this program! ");
       printf("Good-bye!");
    
       system("pause");
    }
    Last edited by Guti14; 08-23-2004 at 11:25 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why does this program say 1700 is a leap year?
    By newbcore in forum C Programming
    Replies: 7
    Last Post: 12-19-2008, 02:03 AM
  2. Basic C input output program help
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 01-27-2008, 06:41 PM
  3. Help with Day Month Year Program..due by 12
    By jgassen15 in forum C Programming
    Replies: 1
    Last Post: 12-06-2007, 11:21 PM
  4. Help! Right Math, Wrong Output
    By verd in forum C Programming
    Replies: 12
    Last Post: 03-15-2005, 07:49 PM
  5. HELP with starting out.
    By sworc66 in forum C Programming
    Replies: 4
    Last Post: 09-05-2002, 10:09 AM