0
0
GoProgramBeginner · 2 min read

Go Program to Check Leap Year with Output and Explanation

In Go, you can check a leap year by using if (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0) to decide if the year is leap or not.
📋

Examples

Input2000
Output2000 is a leap year
Input1900
Output1900 is not a leap year
Input2024
Output2024 is a leap year
🧠

How to Think About It

To check if a year is leap, first see if it divides evenly by 400. If yes, it is leap. If not, check if it divides evenly by 4 but not by 100. If yes, it is leap. Otherwise, it is not leap.
📐

Algorithm

1
Get the input year.
2
Check if the year is divisible by 400; if yes, it is leap.
3
Else, check if the year is divisible by 4 and not divisible by 100; if yes, it is leap.
4
Otherwise, it is not a leap year.
5
Print the result.
💻

Code

go
package main

import "fmt"

func main() {
    year := 2024
    if (year%400 == 0) || (year%4 == 0 && year%100 != 0) {
        fmt.Printf("%d is a leap year\n", year)
    } else {
        fmt.Printf("%d is not a leap year\n", year)
    }
}
Output
2024 is a leap year
🔍

Dry Run

Let's trace the year 2024 through the code

1

Set year

year = 2024

2

Check divisibility by 400

2024 % 400 = 24 (not zero)

3

Check divisibility by 4 and not by 100

2024 % 4 = 0 and 2024 % 100 = 24 (not zero), condition true

4

Print result

"2024 is a leap year"

CheckResult
2024 % 400 == 0false
2024 % 4 == 0true
2024 % 100 != 0true
Leap year?true
💡

Why This Works

Step 1: Divisible by 400 means leap year

If year % 400 == 0, the year is a leap year because every 400 years we add a leap day.

Step 2: Divisible by 4 but not 100 means leap year

If the year is divisible by 4 but not by 100, it is a leap year to correct the calendar.

Step 3: Otherwise not leap year

If neither condition is true, the year is not leap because it doesn't fit leap year rules.

🔄

Alternative Approaches

Using a function to check leap year
go
package main

import "fmt"

func isLeapYear(year int) bool {
    return (year%400 == 0) || (year%4 == 0 && year%100 != 0)
}

func main() {
    year := 1900
    if isLeapYear(year) {
        fmt.Printf("%d is a leap year\n", year)
    } else {
        fmt.Printf("%d is not a leap year\n", year)
    }
}
This approach improves readability and reusability by separating logic into a function.
Using switch statement
go
package main

import "fmt"

func main() {
    year := 2000
    switch {
    case year%400 == 0:
        fmt.Printf("%d is a leap year\n", year)
    case year%4 == 0 && year%100 != 0:
        fmt.Printf("%d is a leap year\n", year)
    default:
        fmt.Printf("%d is not a leap year\n", year)
    }
}
Using switch makes the code cleaner when checking multiple conditions.

Complexity: O(1) time, O(1) space

Time Complexity

The program performs a fixed number of arithmetic and logical operations regardless of input size, so it runs in constant time O(1).

Space Complexity

Only a few variables are used, so the space complexity is constant O(1).

Which Approach is Fastest?

All approaches use simple arithmetic checks and run in constant time; using a function or switch is mostly for code clarity, not speed.

ApproachTimeSpaceBest For
Inline if-elseO(1)O(1)Simple quick checks
Function encapsulationO(1)O(1)Code reuse and clarity
Switch statementO(1)O(1)Readable multiple condition checks
💡
Use parentheses to group conditions clearly when combining && and || operators.
⚠️
Beginners often forget to check the 'not divisible by 100' condition, causing incorrect leap year results.