FOR FREE YEAR SOLVED


Description

A year is taken as input to check whether it is a leap year or not. The conditions for a year to be a leap year are stated below:

  • The year is divisible by 4 but not divisible by 100.
  • For the century years, the years should be divisible by 4 as well as 400 to be a leap year.
  • If the year is not divisible by 4 then it is not a leap year.
  • For the century years, if it is divisible by 4 and 100 but not by 400 then that year will not be a leap year.

For example, if we consider the year 2000, it is divisible by 4 as well as 400; therefore, this year is a leap year.

If we consider the year 1900, it is divisible by 4 as well as by 100. But, the year is not divisible by 400. Therefore, the year 1900 is not a leap year.

If the year is 2016, it is not a century year, but it is divisible by 4. So, 2016 is a leap year.

If the year is 2015, it is not divisible by 4, so, it is not a leap year. 

Algorithm

INPUT: A year

OUTPUT: Whether the year is a leap year or not.

PROCESS:

Step 1: [taking the input]

               Read y [the year to check for leap year]

Step 2: [Checking for leap year]

                              If y Mod 4 = 0 then

                                             If y Mod 100 = 0 then

                                                            If y Mod 400 = 0 then

                                                                          Print "y is a Leap Year"

                                                            Else

                                                                          Print "y is not a Leap Year"

                                                            [End of ‘if’]

                                             Else

                                                            Print "y is a Leap Year"

                                             [End of ‘if’]

                              Else

                                             Print "y is not a Leap Year"

                              [End of ‘if’]

Step 3: Stop.

Code

TIME COMPLEXITY:

               if(y % 4 == 0)-----------------------------------O(1)

               {

                              //if the year is divisible by 400

                              //then it is a leap year else not

               if( y % 100 == 0)---------------------------O(1)

               {

                              if ( y % 400 == 0)---------------O(1)

                                            printf("%d is a Leap Year", y);

                              else-------------------------------O(1)

                                            printf("%d is not a Leap Year", y);

               }

               //if the year is not divisible by 100 but divisible by 4

               //then it is a leap year

               else-----------------------------------------O(1)

                              printf("%d is a Leap Year", y );

               }

    //if the year is not divisible by 4 

    //then it is not a leap year

    else--------------------------------------------O(1)

        printf("%d is not a Leap Year", y);

TIME COMPLEXITY:

The time complexity of this program is O(1) as it needs a constant time to execute.

SPACE COMPLEXITY:

The space complexity of this program is O(1) as it requires same number of memory spaces to execute for any given input.