Q:

Golang program to print the Hex value of numbers from 1 to 100 using the goto statement

belongs to collection: Golang goto Statement Programs

0

In this program, we will print the hexadecimal number corresponding to the decimal number from 1 to 100 on the console screen.

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 Hex value of numbers from 1 to 100 using the goto statement is given below. The given program is compiled and executed successfully.

// Golang program to print the Hex value of numbers
// from 1 to 100 using goto statement.

package main

import "fmt"

func main() {
	var val int = 1

XYZ:
	fmt.Printf("%X ", val)
	val = val + 1

	if val <= 100 {
		goto XYZ
	}
}

Output:

1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 
1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 
2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 
40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 
53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 61 62 63 64

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 a variable val with an initial value 1 and created a label XYZ.

After that increase the value of variable val by 1 till it becomes 100 and print corresponding hexadecimal values using the "%X" format specifier on the console screen,

if (val<=100){
    goto XYZ
}

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

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

total answers (1)

Golang program to print the Octal value of numbers... >>
<< Golang program to read the name and print it 5 tim...