Go program to reverse the number entered by the user, then displays the reversed number on the screen.
For example, if the user enters 678 as input, 876 is displayed as output. We use the mod(%) operator in the program to get the digits of an integer.
Go Lang Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package main import ( "fmt" ) func main() { var n int var reverse int = 0 fmt.Println("Enter a number") fmt.Scanln(&n) // Take input from user for n != 0 { reverse = reverse * 10 reverse = reverse + n%10 n = (n / 10) } fmt.Println(reverse) } |
Output:
1 2 3 4 5 |
Enter a number 12345 54321 |