Swift program to check leap year

SwiftServer Side ProgrammingProgramming

This tutorial will discuss how to write swift program to check leap year

Leap year is a year in which February has 29 days instead of 28 days. Or we can say a leap year has 366 day instead of 365 day. A leap year comes after every four years. In this yea r, an additional day is added to the calendar to keep the year synchronised with astronomical or seasonal year.

Using the following conditions we can check if the given year is a leap year or not.

If a year is completely divisible by 4, then the year is leap year

If a year is completely divisible by 400, then the year is leap year.

If a year is not completely divisible by 100, then the year is not leap year.

If a year is completely divisible by 100, then that year is leap year if the year is also complet ely divisible by 400.

Here we use operator to check the divisibility of the year by 4. This operator will provide the reminder.

Below is a demonstration of the same −

Suppose our given input is −

Year = 1988

The desired output would be −

Yes the given year is leap year

Here 1988 is completely divisible by 4 and leaves 0 remainder.

Suppose our given input is −

Year = 1999

The desired output would be −

No the given year is not a leap year

Here 1999 is not completely divisible by 4 because it leaves 3 remainder.

Algorithm

Following is the algorithm −

  • Step 1− Declare a variable to store the year. Here the year can be pre defined or user defined.

  • Step 2− Check if the given year is completely divisible by 4 or not. If the reminder is 0 then the year is leap year. Otherwise the year is not leap year.

if (myYear %4 = = 0)
  • Step 3− Again check if the given year is completely divisible by 400 or not. If the reminder is 0 then the year is leap year. Otherwise the year is not leap year.

if (myYear %400 = = 0)
  • Step 4− Again check if the given year is completely divisible by 100 or but not divisible by 400, then the year is not leap year.

if (myYear % 100 = = 0 && myYear %400 != 0)
  • Step 5− Print the output

Example 1

The following program shows how to check a leap year.

import Foundation import Glibc let myYear = 1999 print("Entered Year = ",myYear) // If a year is completely divisible by 4 // Then the year is leap year if (myYear % 4 == 0) { // If the year is completely divisible by 100 but // not divisible by 400 then the year is not a leap year if (myYear % 100 == 0 && myYear % 400 != 0) { print("The specified year is not a leap year") } else { print("The specified year is a leap year") } } else{ print("The specified year is not a leap year") }

Output

Entered Year = 1999
The specified year is not a leap year

Here in the above code, we have year 1999. Now we check if the given year is a leap year or not. To check if the given year fulfil all the conditions we use nested if else statement. So the control first enters into the outer if else statement that is −

if (myYear % 4 = = 0)
Here 1999 % 4 = 3; 3==0

So here the condition is false, so the control goes to the respective else statement of the outer if and display "The specified year is not a leap year".

Example 2

The following program shows how to check a leap year.

import Foundation import Glibc // Function to check for leap year func CheckingLeapYear(myYear: Int) -> Bool{ if (myYear % 4 == 0){ return true } else if (myYear % 400 == 0){ return true } else if (myYear % 100 == 0) { return false } else if (myYear % 100 == 0 && myYear % 400 == 0){ return true } else{ return false } } var mYear = 2400 print("Year = ", mYear) if (CheckingLeapYear(myYear: mYear) == true){ print("\(mYear) is a Leap year") } else{ print("\(mYear) is not a Leap year") }

Output

Year = 2400
2400 is a Leap year

Here in the above code, we create a function named C heckingLeapYear() to check if the given year is a leap year or not. In this function, we defined every possible condition to check the given year is leap year using if else ladder −

if (myYear %4 = =0){
   return true
}  
else if (myYear %400 = =0){
   return true
}   
else if (myYear %100 = =0){
   return false
}   
else if (myYear %100 = = 0 && myYear %400 = =0){
   return true
} else{
   return false
}

Here the year = 2400. Hence 2400 is completely divisible by 4 as well as 100 and 400 so the year is leap year.

raja
Updated on 10-Oct-2022 12:35:56

Advertisements