- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
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!
Advertisements