Browse Source

Add first code of chapter 02

T. Meissner 1 week ago
parent
commit
fb3f0a1668

+ 26
- 0
chapter_02/activity_2.01/main.go View File

@@ -0,0 +1,26 @@
1
+package main
2
+
3
+import (
4
+  "fmt"
5
+  "strconv"
6
+)
7
+
8
+func main() {
9
+
10
+  var line string
11
+
12
+  for i := 1; i <= 100; i++ {
13
+    if i%3 == 0 {
14
+      line += "Fizz"
15
+    }
16
+    if i%5 == 0 {
17
+      line += "Buzz"
18
+    }
19
+    if len(line) == 0 {
20
+      line = strconv.Itoa(i)
21
+    }
22
+    fmt.Println(line)
23
+    line = ""
24
+  }
25
+
26
+}

+ 16
- 0
chapter_02/exercise_2.01/main.go View File

@@ -0,0 +1,16 @@
1
+package main
2
+
3
+import "fmt"
4
+
5
+func main() {
6
+
7
+  input := 5
8
+
9
+  if input%2 == 0 {
10
+    fmt.Println(input, "is even")
11
+  }
12
+  if input%2 == 1 {
13
+    fmt.Println(input, "is odd")
14
+  }
15
+
16
+}

+ 15
- 0
chapter_02/exercise_2.02/main.go View File

@@ -0,0 +1,15 @@
1
+package main
2
+
3
+import "fmt"
4
+
5
+func main() {
6
+
7
+  input := 4
8
+
9
+  if input%2 == 0 {
10
+    fmt.Println(input, "is even")
11
+  } else {
12
+    fmt.Println(input, "is odd")
13
+  }
14
+
15
+}

+ 17
- 0
chapter_02/exercise_2.03/main.go View File

@@ -0,0 +1,17 @@
1
+package main
2
+
3
+import "fmt"
4
+
5
+func main() {
6
+
7
+  input := -10
8
+
9
+  if input < 0 {
10
+    fmt.Println("input can't be a negative number")
11
+  } else if input%2 == 0 {
12
+    fmt.Println(input, "is even")
13
+  } else {
14
+    fmt.Println(input, "is odd")
15
+  }
16
+
17
+}

+ 34
- 0
chapter_02/exercise_2.04/main.go View File

@@ -0,0 +1,34 @@
1
+package main
2
+
3
+import (
4
+  "errors"
5
+  "fmt"
6
+)
7
+
8
+func validate(input int) error {
9
+
10
+  if input < 0 {
11
+    return errors.New("input can't be a negatice number")
12
+  } else if input > 100 {
13
+    return errors.New("input can't be over 100")
14
+  } else if input%7 == 0 {
15
+    return errors.New("input can't be divisible by 7")
16
+  } else {
17
+    return nil
18
+  }
19
+
20
+}
21
+
22
+func main() {
23
+
24
+  input := 21
25
+
26
+  if err := validate(input); err != nil {
27
+    fmt.Println(err)
28
+  } else if input%2 == 0 {
29
+    fmt.Println(input, "is even")
30
+  } else {
31
+    fmt.Println(input, "is odd")
32
+  }
33
+
34
+}