Golang程序 读取一个数字(n)并计算(n+nn+nnn)

Golang程序 读取一个数字(n)并计算(n+nn+nnn)

我们来读一个数字,n=5

然后,nn=55,然后nnn=555

res = 5 + 55 + 555 => 615

要读取一个数字(n)并计算(n+nn+nnn),我们可以采取以下方法

步骤

  • 定义一个变量,n。
  • 打印一条语句来获得数字n。
  • 接受用户对变量n的输入。
  • 为(n + nn + nnn)做一个表达式。
  • 将表达式转换为数字。
  • 计算表达式的总和。

例子

package main
import (
   "fmt"
   "strconv"
)
func main(){
   var n int
   fmt.Print("Enter value of n: ")
   fmt.Scanf("%d", &n)
   t1 := fmt.Sprintf("%d", n)
   t2 := fmt.Sprintf("+%d%d", n, n)
   t3 := fmt.Sprintf("+%d%d%d", n, n, n)
   exp := t1 + t2 + t3
   fmt.Println("Expression is: ", exp)
   n1, _ := strconv.Atoi(t1)
   n2, _ := strconv.Atoi(t2)
   n3, _ := strconv.Atoi(t3)
   fmt.Println("Computed expression is: ", n1+n2+n3)
}

输出

Enter value of n: 5
Expression is: 5+55+555
Computed expression is: 615

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程