
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Compute n + nn + nnn in Go
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
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
Advertisements