Golang Program to Check Whether a Given Year is a Leap Year



Steps

  • Take the value of the year as input.
  • Using an if-statement, check whether the year is a leap year or not
  • Print the final result.
Enter the year to be checked: 2016
The year is a leap year!
Enter the year to be checked: 2005
The year isn't a leap year!

Explanation

  • User must first enter the year to be checked.
  • The if statement checks if the year is a multiple of 4 but isn't a multiple of 100 or if it is a multiple of 400 (not every year that is a multiple of 4 is a leap year).
  • Then, the result is printed.

Example

 Live Demo

package main import "fmt" func main() {    var year int    fmt.Print("Enter the year to be checked:")    fmt.Scanf("%d", &year)    if year%4==0 && year%100!=0 || year%400==0{       fmt.Println("The year is a leap year!")    }else{       fmt.Println("The year isn't a leap year!")    } }

Output

Enter the year to be checked:2016 The year is a leap year!
Updated on: 2021-07-31T15:31:40+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements