Last active
June 27, 2022 18:15
-
-
Save ahmadsb86/a7df71d25b41d024fc5bff76d3d12e9e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import( | |
"fmt" | |
"math" | |
) | |
func main(){ | |
//Input (hardcode values if you are using Go Playground since it doesn't support terminal input) | |
var inputs [2]int | |
fmt.Print("\nNumber: ") | |
fmt.Scanf("%d\n", &inputs[0]) | |
fmt.Print("Length: ") | |
fmt.Scanf("%d\n",&inputs[1]) | |
//Output | |
dash := "\n----------" | |
fmt.Println(dash, "\nFirst Method:") | |
oldMethod(inputs[0], inputs[1]) | |
fmt.Println(dash, "\n", dash, "\nSecond Method:") | |
newMethod(inputs[0]) | |
fmt.Println(dash) | |
} | |
func oldMethod(num, len int){ | |
for i:=0; i<len; i++{ | |
fmt.Print(num/pow(10,i) %10) | |
} | |
} | |
func newMethod(num int){ | |
var prev int | |
for i:=0;;i++{ | |
if prev==num{ | |
return | |
}else{ | |
remainder:=((num-prev)/pow(10,i)) %10 | |
prev += remainder*pow(10,i) | |
fmt.Print(remainder) | |
} | |
} | |
} | |
//Inna lillahi wa inna ilayhi raji'un | |
func pow(base, exp int) int{ | |
return int(math.Pow(float64(base), float64(exp))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment