/* * Read two numbers and print the first one in the base * indicated by the second. */ package main import "fmt" func main() { // Declare two integers variables. var num, base int fmt.Printf("Please enter number and base: ") fmt.Scan(&num, &base) /* Find the largest power of the base <= the number, but not less than one. */ pwr := base for ; pwr <= num; pwr *= base { } pwr = pwr / base fmt.Printf("%d in base %d is ", num, base) /* This for loop is like a while in C or Java. It generates and prints the digits. */ for pwr >= 1 { digit := num / pwr num -= digit*pwr pwr /= base if digit < 10 { fmt.Printf("%d", digit) } else { fmt.Printf("%c", 'A'+digit-10) } } fmt.Printf("\n") }