Menu Close

Go – Program to add two numbers in go

Here, we will see program to add two numbers in go. Given below, you can find algorithm and program.

Given two values in variables let us say a and b. We have to add these two values in go.

Algorithm:

  • Store first value in variable in a and second value in variable b.
  • Add these two variables value and store into third variable.
  • Return this final value

Input : 
a = 4
b = 10

Output : 
c = 14

Input : 
a = 5
b = -3


Output :
c = 2

Code:

package main

import (

  "fmt"

)

func add_values(a int, b int) int {

  var c = a + b

  return c
}

func main() {

  var a, b, c int

  fmt.Println("Enter 1st Number: ")

  fmt.Scan(&a)

  fmt.Println("Enter 2nd Number: ")

  fmt.Scan(&b)

  c = add_values(a , b)

  fmt.Printf("Sum: %d \n", c)

}

Output:

Enter 1st Number:
10
Enter 2nd Number:
20
Sum: 30
package main
 
import (
 
  "fmt"
 
)
 
func add_values(a int, b int) int {
 
  var c = a + b
 
  return c
}
 
func main() {
 
  var a = 4
 
  var b = 10
 
  var c = add_values(a, b)
 
  fmt.Printf("Sum: %d \n", c)
 
  a = 5
 
  b = -2
 
  c = add_values(a , b)
 
  fmt.Printf("Sum: %d \n", c)
 
}

Output:


Sum: 14

Sum: 3

To learn more about golang. Please follow given below link.

https://www.techieindoor.com/go-lang/

References:

https://golang.org/doc/
https://golang.org/pkg/

Posted in golang, golang program

Leave a Reply

Your email address will not be published. Required fields are marked *