add: questions and exercises for the if statement
This commit is contained in:
36
11-if/exercises/08/solution/main.go
Normal file
36
11-if/exercises/08/solution/main.go
Normal file
@@ -0,0 +1,36 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) != 2 {
|
||||
fmt.Println("Give me a year number")
|
||||
return
|
||||
}
|
||||
|
||||
year, err := strconv.Atoi(os.Args[1])
|
||||
if err != nil {
|
||||
fmt.Printf("%q is not a valid year.\n", os.Args[1])
|
||||
return
|
||||
}
|
||||
|
||||
if year%4 == 0 && (year%100 != 0 || year%400 == 0) {
|
||||
fmt.Printf("%d is a leap year.\n", year)
|
||||
} else {
|
||||
fmt.Printf("%d is not a leap year.\n", year)
|
||||
}
|
||||
}
|
||||
|
||||
// Review the original source code here:
|
||||
// https://github.com/golang/go/blob/ad644d2e86bab85787879d41c2d2aebbd7c57db8/src/time/time.go#L1289
|
Reference in New Issue
Block a user