diff --git a/chapter_02/activity_2.01/main.go b/chapter_02/activity_2.01/main.go new file mode 100644 index 0000000..f748ef4 --- /dev/null +++ b/chapter_02/activity_2.01/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + "strconv" +) + +func main() { + + var line string + + for i := 1; i <= 100; i++ { + if i%3 == 0 { + line += "Fizz" + } + if i%5 == 0 { + line += "Buzz" + } + if len(line) == 0 { + line = strconv.Itoa(i) + } + fmt.Println(line) + line = "" + } + +} diff --git a/chapter_02/exercise_2.01/main.go b/chapter_02/exercise_2.01/main.go new file mode 100644 index 0000000..26864d9 --- /dev/null +++ b/chapter_02/exercise_2.01/main.go @@ -0,0 +1,16 @@ +package main + +import "fmt" + +func main() { + + input := 5 + + if input%2 == 0 { + fmt.Println(input, "is even") + } + if input%2 == 1 { + fmt.Println(input, "is odd") + } + +} diff --git a/chapter_02/exercise_2.02/main.go b/chapter_02/exercise_2.02/main.go new file mode 100644 index 0000000..85343a0 --- /dev/null +++ b/chapter_02/exercise_2.02/main.go @@ -0,0 +1,15 @@ +package main + +import "fmt" + +func main() { + + input := 4 + + if input%2 == 0 { + fmt.Println(input, "is even") + } else { + fmt.Println(input, "is odd") + } + +} diff --git a/chapter_02/exercise_2.03/main.go b/chapter_02/exercise_2.03/main.go new file mode 100644 index 0000000..8d42af6 --- /dev/null +++ b/chapter_02/exercise_2.03/main.go @@ -0,0 +1,17 @@ +package main + +import "fmt" + +func main() { + + input := -10 + + if input < 0 { + fmt.Println("input can't be a negative number") + } else if input%2 == 0 { + fmt.Println(input, "is even") + } else { + fmt.Println(input, "is odd") + } + +} diff --git a/chapter_02/exercise_2.04/main.go b/chapter_02/exercise_2.04/main.go new file mode 100644 index 0000000..5578b0b --- /dev/null +++ b/chapter_02/exercise_2.04/main.go @@ -0,0 +1,34 @@ +package main + +import ( + "errors" + "fmt" +) + +func validate(input int) error { + + if input < 0 { + return errors.New("input can't be a negatice number") + } else if input > 100 { + return errors.New("input can't be over 100") + } else if input%7 == 0 { + return errors.New("input can't be divisible by 7") + } else { + return nil + } + +} + +func main() { + + input := 21 + + if err := validate(input); err != nil { + fmt.Println(err) + } else if input%2 == 0 { + fmt.Println(input, "is even") + } else { + fmt.Println(input, "is odd") + } + +}