0
0
GoProgramBeginner · 2 min read

Go Program to Check Armstrong Number

A Go program to check an Armstrong number calculates the sum of each digit raised to the power of the number of digits and compares it to the original number using code like sum += int(math.Pow(float64(digit), float64(numDigits))) inside a loop.
📋

Examples

Input153
Output153 is an Armstrong number
Input9474
Output9474 is an Armstrong number
Input123
Output123 is not an Armstrong number
🧠

How to Think About It

To check if a number is an Armstrong number, first find how many digits it has. Then, for each digit, raise it to the power of the total digits and add all these values together. If the sum equals the original number, it is an Armstrong number.
📐

Algorithm

1
Get the input number.
2
Count the number of digits in the number.
3
Initialize sum to zero.
4
For each digit in the number, raise it to the power of the digit count and add to sum.
5
Compare sum with the original number.
6
If equal, print it is an Armstrong number; otherwise, print it is not.
💻

Code

go
package main

import (
	"fmt"
	"math"
)

func main() {
	number := 153
	numDigits := int(math.Log10(float64(number))) + 1
	sum := 0
	n := number

	for n > 0 {
		digit := n % 10
		sum += int(math.Pow(float64(digit), float64(numDigits)))
		n /= 10
	}

	if sum == number {
		fmt.Printf("%d is an Armstrong number\n", number)
	} else {
		fmt.Printf("%d is not an Armstrong number\n", number)
	}
}
Output
153 is an Armstrong number
🔍

Dry Run

Let's trace the number 153 through the code to check if it is an Armstrong number.

1

Calculate number of digits

number = 153, numDigits = 3 (because 153 has 3 digits)

2

Initialize sum and start loop

sum = 0, n = 153

3

First digit calculation

digit = 153 % 10 = 3, sum += 3^3 = 27, sum = 27, n = 153 / 10 = 15

4

Second digit calculation

digit = 15 % 10 = 5, sum += 5^3 = 125, sum = 152, n = 15 / 10 = 1

5

Third digit calculation

digit = 1 % 10 = 1, sum += 1^3 = 1, sum = 153, n = 1 / 10 = 0

6

Compare sum with original number

sum = 153, number = 153, they are equal

DigitDigit^numDigitsSum after addition
32727
5125152
11153
💡

Why This Works

Step 1: Count digits

We use math.Log10 to find how many digits the number has, which is needed to know the power to raise each digit.

Step 2: Sum of powers

Each digit is raised to the power of the digit count using math.Pow, then added to a running sum.

Step 3: Compare sum and number

If the sum equals the original number, it means the number is an Armstrong number.

🔄

Alternative Approaches

String conversion method
go
package main

import (
	"fmt"
	"math"
	"strconv"
)

func main() {
	number := 9474
	numStr := strconv.Itoa(number)
	numDigits := len(numStr)
	sum := 0

	for _, ch := range numStr {
		digit := int(ch - '0')
		sum += int(math.Pow(float64(digit), float64(numDigits)))
	}

	if sum == number {
		fmt.Printf("%d is an Armstrong number\n", number)
	} else {
		fmt.Printf("%d is not an Armstrong number\n", number)
	}
}
This method converts the number to a string and iterates over each character, which can be easier to read but uses extra memory for the string.
Recursive digit power sum
go
package main

import (
	"fmt"
	"math"
)

func powerSum(n, digits int) int {
	if n == 0 {
		return 0
	}
	digit := n % 10
	return int(math.Pow(float64(digit), float64(digits))) + powerSum(n/10, digits)
}

func main() {
	number := 153
	numDigits := int(math.Log10(float64(number))) + 1
	sum := powerSum(number, numDigits)

	if sum == number {
		fmt.Printf("%d is an Armstrong number\n", number)
	} else {
		fmt.Printf("%d is not an Armstrong number\n", number)
	}
}
This recursive approach is elegant but may be less efficient for very large numbers due to function call overhead.

Complexity: O(d) time, O(1) space

Time Complexity

The program loops through each digit once, so the time depends linearly on the number of digits, O(d).

Space Complexity

Only a few variables are used regardless of input size, so space complexity is O(1).

Which Approach is Fastest?

The numeric method using modulo and division is fastest and uses less memory than string conversion or recursion.

ApproachTimeSpaceBest For
Modulo and divisionO(d)O(1)Fastest and memory efficient
String conversionO(d)O(d)Easier to read, uses more memory
Recursive methodO(d)O(d)Elegant but less efficient for large inputs
💡
Use math.Log10 to find the number of digits quickly instead of converting to string.
⚠️
Beginners often forget to raise each digit to the power of the total digit count and instead just sum the digits.