Replacing goto

Report a typo

The code below counts the sum of numbers from a range. Altogether, you need to find three sums: the sum of numbers divisible by three, the sum of numbers divisible by five, and the sum of all other numbers. The range is defined by two numbers that you get from standard input. The first number is the beginning of the range, and the second number is the end of it, both numbers included.

The code below contains a goto statement, and you need to rewrite it without using goto.

package main

import "fmt"

func main() {
    var numberRange, end, sum3, sum5, other int
    fmt.Scan(&numberRange, &end)

StartOfLoop:
    switch {
    case numberRange%3 == 0:
        goto amount3
    case numberRange%5 == 0:
        goto amount5
    default:
        goto otherAmount
    }

amount3:
    sum3 += numberRange
    goto EndOfLoop

amount5:
    sum5 += numberRange
    goto EndOfLoop

otherAmount:
    other += numberRange

EndOfLoop:
    numberRange++
    if numberRange <= end {
        goto StartOfLoop
    }

    fmt.Println(sum3)
    fmt.Println(sum5)
    fmt.Println(other)
}

In case of difficulties, just see the hint.

Tip:

package main

import "fmt"

func main() {
    var numberRange, end, sum3, sum5, other int
    fmt.Scan(&numberRange, &?)

    for ? <= end {
        switch {
        case numberRange%3 == 0:
            sum3 += ?
        case ? == 0:
            ? += numberRange
        default:
            other += ?
        }
        numberRange++
    }

    fmt.Println(sum3)
    fmt.Println(?)
    fmt.Println(other)
}

Sample Input 1:

1 5

Sample Output 1:

3
5
7
Write a program in Go
package main

import "fmt"

func main() {
var numberRange, end, sum3, sum5, other int
fmt.Scan(&numberRange, &end)

// put your code here
}
___

Create a free account to access the full topic