adding cs142 code
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
include $(GOROOT)/src/Make.inc
|
||||
|
||||
TARG=binary
|
||||
GOFILES=\
|
||||
binary.go
|
||||
|
||||
include $(GOROOT)/src/Make.cmd
|
||||
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func itob(i int) string {
|
||||
var buf [64]byte
|
||||
j := len(buf)
|
||||
b := 2
|
||||
for i > 0 {
|
||||
j--
|
||||
buf[j] = "0123456789abcdefghipqrstuvwxyz"[i%b]
|
||||
i /= b
|
||||
}
|
||||
|
||||
return string(buf[j:])
|
||||
}
|
||||
|
||||
|
||||
func main() {
|
||||
var i int
|
||||
fmt.Print("please enter a number: ")
|
||||
fmt.Scan(&i)
|
||||
fmt.Printf("%d as a binary: %s\n", i, itob(i))
|
||||
}
|
||||
Reference in New Issue
Block a user