Golang Program to Read a Number (n) and Compute (n+nn+nnn)

Go ProgrammingServer Side ProgrammingProgramming

Let's read a number, n=5

Then, nn=55 and then nnn=555

res = 5 + 55 + 555 => 615

To read a number (n) and compute (n+nn+nnn), we can take the following

Steps


  • Define a variable, n.
  • Print a statement to get the number, n.
  • Take user input for variable, n.
  • Make an expression for (n + nn + nnn).
  • Convert the expression into numbers.
  • Compute the sum of the expression.

Example

 Live Demo

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

Output

Enter value of n: 5
Expression is: 5+55+555
Computed expression is: 615
raja
Published on 31-Jul-2021 14:14:38
Advertisements