Sha256: c659fed0d1ff7ae04751367d949b585c0f649e8cb6f7f46fafca8345889043b1

Contents?: true

Size: 1.71 KB

Versions: 10

Compression:

Stored size: 1.71 KB

Contents

===int===
package main

import "fmt"

func main() {
    var a, b int
    fmt.Print("enter two integers: ")
    fmt.Scanln(&a, &b)
    fmt.Printf("%d + %d = %d\n", a, b, a+b)
    fmt.Printf("%d - %d = %d\n", a, b, a-b)
    fmt.Printf("%d * %d = %d\n", a, b, a*b)
    fmt.Printf("%d / %d = %d\n", a, b, a/b)  // truncates towards 0
    fmt.Printf("%d %% %d = %d\n", a, b, a%b) // same sign as first operand
    // no exponentiation operator
}
{{out|Example run}}

enter two integers: -5 3
-5 + 3 = -2
-5 - 3 = -8
-5 * 3 = -15
-5 / 3 = -1
-5 % 3 = -2

===big.Int===
package main

import (
    "fmt"
    "math/big"
)

func main() {
    var a, b, c big.Int
    fmt.Print("enter two integers: ")
    fmt.Scan(&a, &b)
    fmt.Printf("%d + %d = %d\n", &a, &b, c.Add(&a, &b))
    fmt.Printf("%d - %d = %d\n", &a, &b, c.Sub(&a, &b))
    fmt.Printf("%d * %d = %d\n", &a, &b, c.Mul(&a, &b))

    // Quo, Rem functions work like Go operators on int:
    // quo truncates toward 0,
    // and a non-zero rem has the same sign as the first operand.
    fmt.Printf("%d quo %d = %d\n", &a, &b, c.Quo(&a, &b))
    fmt.Printf("%d rem %d = %d\n", &a, &b, c.Rem(&a, &b))

    // Div, Mod functions do Euclidean division:
    // the result m = a mod b is always non-negative,
    // and for d = a div b, the results d and m give d*y + m = x.
    fmt.Printf("%d div %d = %d\n", &a, &b, c.Div(&a, &b))
    fmt.Printf("%d mod %d = %d\n", &a, &b, c.Mod(&a, &b))

    // as with int, no exponentiation operator
}
{{out|Example run}}

enter two integers: -5 3
-5 + 3 = -2
-5 - 3 = -8
-5 * 3 = -15
-5 quo 3 = -1
-5 rem 3 = -2
-5 div 3 = -2
-5 mod 3 = 1

Version data entries

10 entries across 7 versions & 1 rubygems

Version Path
zettacode-0.1.7 files.zettacode/arithmetic.integer/go.txt
zettacode-0.1.6 files.zettacode/arithmetic.integer/go.txt
zettacode-0.1.6 files.zettacode2/arithmetic.integer/go.txt
zettacode-0.1.5 files.zettacode/arithmetic.integer/go.txt
zettacode-0.1.5 files.zettacode2/arithmetic.integer/go.txt
zettacode-0.1.4 files.zettacode/arithmetic.integer/go.txt
zettacode-0.1.4 files.zettacode2/arithmetic.integer/go.txt
zettacode-0.1.3 files.zettacode/arithmetic.integer/go.txt
zettacode-0.1.2 files.zettacode/arithmetic.integer/go.txt
zettacode-0.1.1 zettacode.files/arithmetic.integer/go.txt