ADVERTISEMENT
ADVERTISEMENT

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

Here, we are going to learn how to print the Hex value of numbers from 1 to 100 using the goto statement in Golang (Go Language)?
Submitted by Nidhi, on February 21, 2021

Problem Solution:

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

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.

Golang goto Statement Programs »



ADVERTISEMENT




ADVERTISEMENT


Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.