Sunday, August 4, 2013

Program to determine whether the year is a leap year or not :: Logic building (2)


Before writing the program for finding Leap year, I just want to give you a brief history about the leap year and what it is.

It takes the Earth approximately 365 days, 5 hours, 48 minutes, and 46 seconds to circle once around the Sun. Leap Years are needed to keep our calendar alignment with the Earth's revolutions around the sun. So if we didn't add a day on February 29 nearly every 4 years, we would lose almost six hours off our calendar every year. After only 100 years, our calendar would be off by approximately 24 days!



To determine whether a year is a leap year or not , follow the below mentioned algorithm

Algorithm

1.       If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5.
2.       If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.
3.       If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5.
4.       The year is a leap year (it has 366 days).
5.       The year is not a leap year (it has 365 days).



If the value in cell A1 is this        The formula returns
   ----------------------------------------------------------
   1992                                   Leap Year
   2000                                   Leap Year
   1900                                   NOT a Leap Year


Program to determine whether the year is a leap year or not


      int inputYear;
      printf("Please Enter Year");
      scanf("%d",&inputYear);
      if(inputYear%4 == 0)
            if(inputYear%100 == 0 )
                  {
                        if(inputYear%400 == 0)
                              printf("%d is a Leap Year",inputYear);
                              else
                              printf("%d is  not Leap Year",inputYear);}
            else
            {
                  printf("%d is Leap Year",inputYear);
            }
      else
      {
            printf("%d is not Leap Year",inputYear);


      }


No comments:

Post a Comment