Browse Source

Add first code of chapter 02

master
T. Meissner 4 years ago
parent
commit
fb3f0a1668
5 changed files with 108 additions and 0 deletions
  1. +26
    -0
      chapter_02/activity_2.01/main.go
  2. +16
    -0
      chapter_02/exercise_2.01/main.go
  3. +15
    -0
      chapter_02/exercise_2.02/main.go
  4. +17
    -0
      chapter_02/exercise_2.03/main.go
  5. +34
    -0
      chapter_02/exercise_2.04/main.go

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

@ -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 = ""
}
}

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

@ -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")
}
}

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

@ -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")
}
}

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

@ -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")
}
}

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

@ -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")
}
}

Loading…
Cancel
Save