Files
learngo/13-loops/exercises/07-multiplication-table-exercises/01-dynamic-table/solution/main.go

48 lines
768 B
Go
Raw Normal View History

2018-10-13 23:30:21 +03:00
// 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"
2018-10-22 12:25:30 +03:00
"os"
"strconv"
2018-10-13 23:30:21 +03:00
)
2018-10-22 12:25:30 +03:00
func main() {
args := os.Args
2018-10-13 23:30:21 +03:00
2018-10-22 12:25:30 +03:00
if len(args) != 2 {
fmt.Println("Give me the size of the table")
return
}
size, err := strconv.Atoi(args[1])
if err != nil || size <= 0 {
fmt.Println("Wrong size")
return
}
2018-10-13 23:30:21 +03:00
// print the header
fmt.Printf("%5s", "X")
2018-10-22 12:25:30 +03:00
for i := 0; i <= size; i++ {
2018-10-13 23:30:21 +03:00
fmt.Printf("%5d", i)
}
fmt.Println()
2018-10-22 12:25:30 +03:00
for i := 0; i <= size; i++ {
2018-10-13 23:30:21 +03:00
// print the vertical header
fmt.Printf("%5d", i)
// print the cells
2018-10-22 12:25:30 +03:00
for j := 0; j <= size; j++ {
2018-10-13 23:30:21 +03:00
fmt.Printf("%5d", i*j)
}
fmt.Println()
}
}