Go number programs check special properties of numbers, such as whether a number is prime, a palindrome or an Armstrong number. These are some of the most asked questions in coding rounds for freshers and experienced developers alike. In this post, you will write thirteen number programs with their exact output, and then practice the tricky edge cases interviewers like to test.
You will use everything from our Go loop programs and Go if else programs, so finish those first. We also split each check into a small function, which our Go functions guide explains. All programs were tested on Go 1.27.
What are Go Number Programs?
Go number programs take a whole number and test a rule about its digits or divisors. For example, a prime number has exactly two divisors, and a palindrome number reads the same both ways.
Think of these programs as detectives. Each one asks a different question about a number, collects clues with % and /, and gives a yes or no answer. Once you learn the clue tools, you can solve almost any number puzzle.
Why Go Number Programs Matter
Go number programs look like school maths, but they teach real skills. You learn to break a number into digits, to loop only as far as needed, and to handle edge cases such as 0, 1 and negative numbers.
- Coding rounds: prime, palindrome and Armstrong checks appear in almost every fresher test.
- Problem solving: they train you to think about loops, limits and speed.
- Real use: GCD helps simplify fractions, and base conversion is used in networking and file formats.
How Go Number Programs Work
Most Go number programs use two simple tools. n % 10 gives the last digit, and n / 10 removes it. Repeat these in a loop, and you can visit every digit from right to left.

The table below lists the number types in this post with an example of each.
| Number type | Rule | Example |
|---|---|---|
| Prime | Only divisible by 1 and itself | 29 |
| Palindrome | Same digits forwards and backwards | 1221 |
| Armstrong | Sum of digits raised to digit count equals the number | 153 |
| Perfect | Sum of proper divisors equals the number | 28 |
| Strong | Sum of digit factorials equals the number | 145 |
Prime Go Number Programs
Prime numbers are the most popular number questions, so we start with them.
Program 1: Check if a Number is Prime
We only need to test divisors up to the square root of the number. If a number had a larger factor, it would also have a smaller one.
package main
import "fmt"
// isPrime returns true when number has no divisors other than 1 and itself
func isPrime(number int) bool {
// Step 1: 0, 1 and negative numbers are not prime
if number < 2 {
return false
}
// Step 2: try every divisor up to the square root
// i*i <= number is the same as i <= square root of number
for i := 2; i*i <= number; i++ {
if number%i == 0 {
return false // found a divisor, so it is not prime
}
}
// Step 3: no divisor found
return true
}
func main() {
numbers := []int{1, 2, 17, 21, 97}
for _, number := range numbers {
fmt.Println(number, "prime:", isPrime(number))
}
}
1 prime: false
2 prime: true
17 prime: true
21 prime: false
97 prime: true
The function first throws out numbers below 2. Then it tries each divisor from 2 upward and stops at the first one it finds. i*i <= number is the square root check without using floats. Also, 1 is not prime, because it has only one divisor.
Program 2: All Primes up to 50 with a Sieve
The Sieve of Eratosthenes finds all primes in a range quickly. It crosses out the multiples of each prime.
package main
import "fmt"
func main() {
limit := 50
// make creates a slice of 51 bools, all false at the start.
// composite[x] becomes true when x is NOT prime.
composite := make([]bool, limit+1)
// Step 1: cross out the multiples of each prime
for i := 2; i*i <= limit; i++ {
if !composite[i] { // ! means "not", so this reads "if i is still prime"
for j := i * i; j <= limit; j += i {
composite[j] = true
}
}
}
// Step 2: print every number that was never crossed out
for number := 2; number <= limit; number++ {
if !composite[number] {
fmt.Print(number, " ")
}
}
fmt.Println()
}
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
For 2, the program crosses out 4, 6, 8 and so on. Then it does the same for 3, 5 and 7. composite[j] = true marks a number that has a smaller factor. Every number left unmarked is prime.
Digit-Based Go Number Programs
These Go number programs break a number into its digits.
Program 3: Print the Digits of a Number
This easy first version shows the two digit tools from the diagram above. It prints each digit from right to left.
package main
import "fmt"
func main() {
n := 4719 // n is the number, same name as in the diagram
for n > 0 {
digit := n % 10 // Step 1: % 10 gives the last digit
n = n / 10 // Step 2: / 10 removes the last digit
fmt.Println("Digit:", digit, "| left:", n)
}
}
Digit: 9 | left: 471
Digit: 1 | left: 47
Digit: 7 | left: 4
Digit: 4 | left: 0
On each pass, n % 10 picks the last digit and n / 10 drops it. So 4719 becomes 471, then 47, then 4 and finally 0. When n reaches 0, the loop stops. Every digit program below is built on this loop.
Program 4: Palindrome Number
We reverse the number and compare it with the original.
package main
import "fmt"
// isPalindrome returns true when number reads the same both ways
func isPalindrome(number int) bool {
if number < 0 {
return false
}
original := number // keep a copy to compare at the end
reversed := 0
for number > 0 {
lastDigit := number % 10
reversed = reversed*10 + lastDigit // add the digit to the end of reversed
number = number / 10
}
return original == reversed
}
func main() {
numbers := []int{121, 1221, 123, 7, -121}
for _, number := range numbers {
fmt.Println(number, "palindrome:", isPalindrome(number))
}
}
121 palindrome: true
1221 palindrome: true
123 palindrome: false
7 palindrome: true
-121 palindrome: false
The loop builds the reversed number one digit at a time. For 123, reversed grows from 3 to 32 to 321. Then the function compares it with the saved copy. Negative numbers are not palindromes, because “-121” reversed is “121-“.
Program 5: Armstrong Number
An Armstrong number equals the sum of its digits, each raised to the number of digits. For 153, that is 1 + 125 + 27.
package main
import "fmt"
// power multiplies base by itself exponent times, using only ints
func power(base int, exponent int) int {
result := 1
for range exponent { // repeat exponent times
result *= base
}
return result
}
// isArmstrong checks if number equals the sum of its digits raised to the digit count
func isArmstrong(number int) bool {
digitCount := len(fmt.Sprint(number)) // Sprint turns 153 into the text "153"
sum := 0
remaining := number // work on a copy so number stays the same
for remaining > 0 {
lastDigit := remaining % 10
sum += power(lastDigit, digitCount)
remaining = remaining / 10
}
return sum == number
}
func main() {
for number := 1; number <= 1000; number++ {
if isArmstrong(number) {
fmt.Print(number, " ")
}
}
fmt.Println()
}
1 2 3 4 5 6 7 8 9 153 370 371 407
For each number, the program counts its digits and then raises each digit to that count. We count digits with len(fmt.Sprint(number)), which turns the number into text. Our own power function keeps the maths in ints, so there are no float rounding errors.
Program 6: Strong Number
A strong number equals the sum of the factorials of its digits. For 145, that is 1! + 4! + 5! = 1 + 24 + 120.
package main
import "fmt"
func main() {
// Step 1: store the factorials of 0 to 9 in an array of 10 ints.
// {1} sets factorials[0] to 1; the rest start at 0.
factorials := [10]int{1}
for i := 1; i < 10; i++ {
factorials[i] = factorials[i-1] * i
}
// Step 2: test each number
numbers := []int{145, 40585, 123}
for _, number := range numbers {
sum := 0
remaining := number
for remaining > 0 {
lastDigit := remaining % 10
sum += factorials[lastDigit] // look up the factorial of this digit
remaining = remaining / 10
}
fmt.Println(number, "strong:", sum == number)
}
}
145 strong: true
40585 strong: true
123 strong: false
We work out the factorials of 0 to 9 once and store them in an array. Then each digit just looks up its value, which is faster. For 145, the sum is 1 + 24 + 120, which matches the number.
Program 7: Neon Number
A neon number equals the sum of the digits of its square. For example, 9 squared is 81, and 8 + 1 = 9.
package main
import "fmt"
// digitSum adds up all the digits of a number
func digitSum(number int) int {
sum := 0
for number > 0 {
sum += number % 10
number = number / 10
}
return sum
}
func main() {
numbers := []int{1, 9, 12, 45}
for _, number := range numbers {
square := number * number
isNeon := digitSum(square) == number // true or false
fmt.Println(number, "square:", square, "neon:", isNeon)
}
}
1 square: 1 neon: true
9 square: 81 neon: true
12 square: 144 neon: false
45 square: 2025 neon: false
First, the program squares each number. Next, digitSum adds up the digits of that square. If the sum equals the original number, it is a neon number. For 12, the digits of 144 add up to 9, so the answer is false.
Program 8: Count, Sum and Product of Digits
This program finds three facts about the digits in one loop.
package main
import "fmt"
func main() {
number := 50721
count := 0
sum := 0
product := 1 // start at 1, because anything times 0 is 0
remaining := number
for remaining > 0 {
digit := remaining % 10
count++
sum += digit
product *= digit
remaining = remaining / 10
}
fmt.Println("Digits:", count, "Sum:", sum, "Product:", product)
}
Digits: 5 Sum: 15 Product: 0
Each pass takes one digit and updates all three totals. The sum is 5 + 0 + 7 + 2 + 1 = 15. The product is 0, because one digit is 0. Interviewers often add a 0 to see if you notice.
Divisor-Based Go Number Programs
These Go number programs look at the numbers that divide n.
Program 9: Factors of a Number
A factor is a number that divides another number with no remainder.
package main
import "fmt"
func main() {
number := 36
fmt.Print("Factors of ", number, ": ")
// Try every number from 1 up to number itself
for i := 1; i <= number; i++ {
if number%i == 0 { // no remainder, so i is a factor
fmt.Print(i, " ")
}
}
fmt.Println()
}
Factors of 36: 1 2 3 4 6 9 12 18 36
The loop tries each value from 1 to 36. When the remainder is 0, that value is a factor, so the program prints it. This simple version is easy to read, but it is slow for big numbers.
Program 10: Perfect Number
A perfect number equals the sum of its proper divisors, which are all divisors except itself.
package main
import "fmt"
// isPerfect returns true when number equals the sum of its proper divisors
func isPerfect(number int) bool {
if number < 2 {
return false
}
sum := 1 // 1 divides every number
for i := 2; i*i <= number; i++ {
if number%i == 0 {
partner := number / i // divisors come in pairs, like 2 and 14 for 28
sum += i
if i != partner { // do not add the same divisor twice (e.g. 6 x 6)
sum += partner
}
}
}
return sum == number
}
func main() {
for number := 1; number <= 10000; number++ {
if isPerfect(number) {
fmt.Print(number, " ")
}
}
fmt.Println()
}
6 28 496 8128
Each divisor i comes with a partner number / i. So we add both and only loop to the square root, which makes the program fast. For 28, the sum is 1 + 2 + 14 + 4 + 7, which is 28.
Program 11: GCD and LCM
The GCD is the largest number that divides both. Euclid’s method finds it by repeated remainders.
package main
import "fmt"
// gcd finds the greatest common divisor with Euclid's method
func gcd(first int, second int) int {
for second != 0 {
remainder := first % second
first = second // the old second becomes the new first
second = remainder // the remainder becomes the new second
}
return first
}
func main() {
first := 48
second := 180
common := gcd(first, second)
lcm := first / common * second // divide first to keep the numbers small
fmt.Println("GCD:", common)
fmt.Println("LCM:", lcm)
}
GCD: 12
LCM: 720
Euclid’s loop keeps replacing the pair with the smaller number and the remainder. When the remainder is 0, the other number is the GCD. We write first / common * second instead of first * second / common. Dividing first keeps the numbers small and avoids overflow.
Base Conversion Go Number Programs
Computers store numbers in binary. These Go number programs move between bases.
Program 12: Decimal to Binary, Octal and Hex
This program shows the manual way first, then the ready-made ways.
package main
import (
"fmt"
"strconv" // strconv converts between numbers and text
)
// toBinary builds the binary text of a number by dividing by 2
func toBinary(number int) string {
if number == 0 {
return "0"
}
bits := ""
for number > 0 {
remainder := number % 2 // 0 or 1
bits = strconv.Itoa(remainder) + bits // Itoa turns 1 into "1"; put it in front
number = number / 2
}
return bits
}
func main() {
number := 45
fmt.Println("Manual binary:", toBinary(number))
fmt.Println("strconv binary:", strconv.FormatInt(int64(number), 2)) // base 2
// %b is binary, %o is octal, %x is hex
fmt.Printf("Printf: %b %o %x\n", number, number, number)
}
Manual binary: 101101
strconv binary: 101101
Printf: 101101 55 2d
The manual way divides by 2 and keeps the remainders. Each new remainder goes in front, so the bits end up in the right order. In real code, strconv.FormatInt or %b does it for you.
Program 13: Binary to Decimal
strconv.ParseInt reads text in any base and gives back a number.
package main
import (
"fmt"
"strconv"
)
func main() {
// ParseInt(text, base, bit size) returns the number and an error value.
// err is the error value; nil means "no error".
number, err := strconv.ParseInt("101101", 2, 64)
fmt.Println(number, err)
// _ ignores the error, because we know "ff" is valid hex
hexNumber, _ := strconv.ParseInt("ff", 16, 64)
fmt.Println(hexNumber)
// "2" is not a binary digit, so this returns an error
_, err = strconv.ParseInt("1021", 2, 64)
fmt.Println(err)
}
45 <nil>
255
strconv.ParseInt: parsing "1021": invalid syntax
The second argument is the base, so 2 means binary and 16 means hex. When the text is valid, err is nil. A “2” is not a valid binary digit, so the last call returns an error.
Go Number Programs: Loop to n vs Square Root
Speed matters in Go number programs, because inputs can be large. The diagram and table below compare a full loop with a square root loop.

| Feature | Loop to n | Loop to square root |
|---|---|---|
| Steps for n = 1,000,000 | About 1,000,000 | About 1,000 |
| Correct for prime check | Yes | Yes |
| Condition | i < n | i*i <= n |
| Finds all divisors | Yes, directly | Yes, with pairs i and n/i |
| Good for big inputs | No | Yes |
So always loop to the square root for prime and divisor problems. It is the first thing interviewers look for.
Common Mistakes in Go Number Programs
- Calling 1 prime: 1 has only one divisor, so it is not prime.
- Changing the original number: copy the number into
remainingbefore the digit loop, or you cannot compare at the end. - Overflow when reversing: reversing a large
int32can overflow, so useintor check limits. - Using math.Pow for digits: it returns
float64, which can cause rounding surprises. Write a small int power function. - Forgetting zero: a loop with
for number > 0never runs for 0, so handle it on its own.
Best Practices for Go Number Programs
- Put each check in its own function, such as
isPrime(number int) bool, so it is easy to test. - Loop to the square root for any divisor problem.
- Handle edge cases first: negative numbers, 0 and 1.
- Use strconv for bases in real code, and the manual way only to show you understand it.
- Test with known answers, such as 153 for Armstrong and 28 for perfect.
Go Number Programs Interview Questions and Answers
Interviewers ask these Go number programs often in coding rounds. Each program is complete, with real output from Go 1.27.
Prime and Divisor Questions
Q1. Why do we loop only to the square root in a prime check?
If n = a x b and both a and b were bigger than the square root, their product would be bigger than n. So at least one factor is small, and we find it before the square root.
Q2. What does this program print?
package main
import "fmt"
func main() {
count := 0
for number := 2; number < 100; number++ {
isPrime := true // assume prime until we find a divisor
for i := 2; i*i <= number; i++ {
if number%i == 0 {
isPrime = false
break // stop the inner loop early
}
}
if isPrime {
count++
}
}
fmt.Println(count)
}
25
There are 25 primes below 100. The break stops the inner loop as soon as a divisor is found, which saves time.
Q3. Is 2 the only even prime?
Yes. Every other even number divides by 2, so it has at least three divisors. That is why many prime checks handle 2 first and then test only odd numbers.
Q4. What does this program print?
package main
import "fmt"
// gcd finds the greatest common divisor with Euclid's method
func gcd(first int, second int) int {
for second != 0 {
remainder := first % second
first = second
second = remainder
}
return first
}
func main() {
fmt.Println(gcd(17, 5), gcd(0, 9), gcd(21, 14))
}
1 9 7
17 and 5 share no factor, so the GCD is 1. The GCD of 0 and any number is that number.
Digit Questions
Q5. What does this program print?
package main
import "fmt"
func main() {
number := 1200
reversed := 0
for number > 0 {
reversed = reversed*10 + number%10
number = number / 10
}
fmt.Println(reversed)
}
21
The trailing zeros become leading zeros, which a number does not keep. So 1200 reversed is 21, and 1200 is not a palindrome.
Q6. How do you check a palindrome number using strings?
package main
import (
"fmt"
"strconv"
)
func main() {
text := strconv.Itoa(12321) // Itoa turns the number into the text "12321"
isPalindrome := true
// i starts at the front, j starts at the back, and they move toward each other
for i, j := 0, len(text)-1; i < j; i, j = i+1, j-1 {
if text[i] != text[j] { // text[i] is the character at position i
isPalindrome = false
break
}
}
fmt.Println(isPalindrome)
}
true
Convert the number to text, then compare the first and last characters while moving inward. It is easy to read, but the maths version uses less memory.
Q7. What does this program print?
package main
import "fmt"
func main() {
number := 9875
// Keep adding the digits until only one digit is left
for number > 9 {
sum := 0
for number > 0 {
sum += number % 10
number = number / 10
}
number = sum
}
fmt.Println(number)
}
2
This finds the digital root. 9+8+7+5 is 29, then 2+9 is 11, then 1+1 is 2.
Q8. Is 9474 an Armstrong number?
Yes. It has 4 digits, and 9^4 + 4^4 + 7^4 + 4^4 = 6561 + 256 + 2401 + 256 = 9474. Armstrong numbers can have any number of digits.
Q9. What does this program print?
package main
import "fmt"
func main() {
number := 0
digits := 0
for number > 0 {
digits++
number = number / 10
}
fmt.Println(digits)
}
0
The loop never runs for 0, so it reports 0 digits. But 0 has one digit, so real code must handle this case separately.
Conversion and Output Questions
Q10. What does this program print?
package main
import (
"fmt"
"strconv"
)
func main() {
// FormatInt(number, base) writes a number in the given base
fmt.Println(strconv.Itoa(255), strconv.FormatInt(255, 16), strconv.FormatInt(-5, 2))
}
255 ff -101
FormatInt works with any base from 2 to 36. For negative numbers, it simply adds a minus sign.
Q11. What does this program print?
package main
import "fmt"
func main() {
// 0b = binary, 0o = octal, 0x = hex; _ is only for easy reading
fmt.Println(0b1010, 0o17, 0x1F, 1_000_000)
}
10 15 31 1000000
Go lets you write numbers in binary with 0b, octal with 0o and hex with 0x. Underscores make big numbers easier to read and do not change the value.
Q12. How do you check if a number is a power of two?
package main
import "fmt"
// isPowerOfTwo uses a bit trick: a power of two has exactly one 1 bit.
// number-1 flips that bit off and turns the lower bits on (8 = 1000, 7 = 0111),
// so number & (number-1) is 0 only for powers of two.
func isPowerOfTwo(number int) bool {
return number > 0 && number&(number-1) == 0
}
func main() {
numbers := []int{1, 8, 12, 64}
for _, number := range numbers {
fmt.Println(number, isPowerOfTwo(number))
}
}
1 true
8 true
12 false
64 true
A power of two has exactly one bit set. number & (number-1) clears that bit, so the result is 0 only for powers of two. For 12, which is 1100 in binary, a bit is left over, so the answer is false.
Q13. What does this program print?
package main
import "fmt"
func main() {
sum := 0
for i := 1; i < 1000; i++ {
if i%3 == 0 || i%5 == 0 { // || means "or"
sum += i
}
}
fmt.Println(sum)
}
233168
This is a classic puzzle: the sum of all multiples of 3 or 5 below 1000. The || makes sure numbers like 15 are counted once.
Q14. How do you find large factorials in Go?
Use the math/big package. A normal uint64 overflows after 20!, but big.Int grows as needed.
package main
import (
"fmt"
"math/big" // big numbers with no size limit
)
func main() {
// new(big.Int) makes an empty big number and returns a pointer to it.
// MulRange(1, 25) multiplies 1 x 2 x ... x 25 and stores the answer.
factorial := new(big.Int).MulRange(1, 25)
fmt.Println(factorial)
}
15511210043330985984000000
MulRange(1, 25) multiplies every number from 1 to 25, which is 25!.
Q15. What is a neon number?
A neon number is one where the sum of the digits of its square equals the number. For example, 9 squared is 81, and 8 + 1 = 9. Program 7 above checks it with the same digit loop as the other programs here.
Practice Task
This task combines two Go number programs from above. Write a program that prints all twin primes below 100. Twin primes are pairs of primes that differ by 2, such as (11, 13). Hint: reuse the isPrime function from Program 1.
Official Resources
Quick Recap
- Go number programs use
n % 10to get a digit andn / 10to remove it. - Loop to the square root for prime and divisor checks.
- Handle 0, 1 and negative numbers before the main logic.
- Use
strconv.FormatIntandParseIntto convert between bases. - Use
math/bigwhen results are too large foruint64.
What’s Next?
Next, we use nested loops to draw shapes. Read Go pattern programs with stars, numbers and pyramids. To revise loops before that, read Go loop programs with output.


