Golang程序 打印一个给定数字的乘法表
操作步骤
- 读取一个数字并将其存储在一个变量中。
- 打印给定数字的乘法表。
输入要打印表的数字。7
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
输入要打印表格的数字。17
17 x 1 = 7
17 x 2 = 34
17 x 3 = 51
17 x 4 = 68
17 x 5 = 85
17 x 6 = 102
17 x 7 = 119
17 x 8 = 136
17 x 9 = 153
17 x 10 = 170
解释
- 用户必须输入一个数字。
- 使用打印语句,打印给定数字的乘法表。
例子
package main
import "fmt"
func main(){
var n int
fmt.Print("Enter the number to print the multiplication table:")
fmt.Scanf("%d", &n)
for i:=1; i<11; i++ {
fmt.Println(n, "x", i, "=", n*i)
}
}
输出
Enter the number to print the multiplication table: 7
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70