-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis_leap_year.go
66 lines (56 loc) · 1.09 KB
/
is_leap_year.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
Refer: Leap year: https://en.wikipedia.org/wiki/Leap_year
Algorithm:
if (year is not divisible by 4) then (it is a common year)
else if (year is not divisible by 100) then (it is a leap year)
else if (year is not divisible by 400) then (it is a common year)
else (it is a leap year
*/
package main
import "fmt"
func isLeapYear(year int)bool{
if(year % 4 != 0 ){
return false
} else if(year % 100 != 0){
return true
} else if(year % 400 != 0){
return false
} else {
return true
}
/*
Another method: compact yet complex
if(((year % 4 == 0) and (year % 100 !=0)) or (year % 400==0))
{
LEAP YEAR
}
else
{
COMMON YEAR;
}
*/
}
func main(){
var year int
fmt.Print("Enter year: ")
fmt.Scan(&year)
if(isLeapYear(year)){
fmt.Println("Leap Year")
} else {
fmt.Println("Common Year")
}
}
/** Outputs
$ go run is_leap_year.go
Enter year: 2018
Common Year
$ go run is_leap_year.go
Enter year: 2020
Leap Year
$ go run is_leap_year.go
1Enter year: 900
Common Year
$ go run is_leap_year.go
Enter year: 2024
Leap Year
*/