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

Here, we are going to learn how to print the table of the given number using the goto statement in Golang (Go Language)?
Submitted by Nidhi, on February 21, 2021 [Last updated : March 03, 2023]

Golang goto Statement Example – Print the table of the given number

Problem Solution:

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

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 code to print the table of the given number using the goto statement

// 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 val, num 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.

Golang goto Statement Programs »






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.