Q:

Golang program to print the table of the given number using the goto statement

belongs to collection: Golang goto Statement Programs

0

In this program, we will read an integer number from the user and print the table of the given number using the goto statement.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to print the table of the given number using the goto statement is given below. The given program is compiled and executed successfully.

// Golang program to print the table of the given number
// using GOTO statement.

package main

import "fmt"

func main() {

	var val int = 0
	var num int = 0

	fmt.Print("Enter Number: ")
	fmt.Scanf("%d", &num)

XYZ:
	val = val + 1

	fmt.Printf("%d*%d=%d\n", num, val, num*val)
	if val < 10 {
		goto XYZ
	}
}

Output:

Enter Number: 2
2*1=2
2*2=4
2*3=6
2*4=8
2*5=10
2*6=12
2*7=14
2*8=16
2*9=18
2*10=20

Explanation:

In the above program, we declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file. Here, we imported the fmt package that includes the files of package fmt then we can use a function related to the fmt package.

In the main() function, we created two variables valnum that are initialized with 0 and created a label XYZ.

After that increase the value of variable val by 1 till it becomes 10.

fmt.Printf("%d*%d=%d\n",num,val,num*val)

The above statement will print the calculated number each time on the console screen,

if (val<10){
    goto XYZ
}

The above code will transfer program control to the XYZ label if the value of variable val is less than equal to 10.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Golang program to read the name and print it 5 tim... >>
<< Golang program to print \'Hello World\' ...