Golang Program to Display Armstrong Number Between Two Intervals


An Armstrong number, also known as a narcissistic number, is a number that is equal to the sum of its own digits each raised to the power of the number of digits. Here we will show different approaches about how we can print the Armstrong numbers in go programming language.

Method 1: By Using Math Package

In this example we will use different functions present in math package in order to print the Armstrong numbers between said limits.

Syntax

func Pow(a, b float64) float64

The pow() function is present in math package and is used to raise one number to the exponential of another. The function takes two numbers as arguments in float64 format and returns the result by performing ab operation.

func Log(x float64) float64

The log() function is present in math package and is used to find the natural logarithm of a number. it takes the number whose natural log is to be calculated as argument in float64 format and returns the final result in the same format.

Algorithm

  • Step 1 − Import the fmt and math package.

  • Step 2 − Start the main() function.

  • Step 3 − Inside the main() initialize two integer variables to hold the numbers

  • Step 4 − Use a for loop to iterate over each number in between the limits.

  • Step 5 − Now, take each digit of the number one by one and add them together by multiplying them with the 10th place number.

  • Step 5 − Print the output

Example

Go language program to display armstrong numbers between two intervals by using math package

package main
import (
   "fmt"
   "math"
)
func main() {
   var n int = 100
   var m int = 1000
   fmt.Println("Armstrong numbers between", n, "and", m, "are:")
   fmt.Println()
   for i := n; i <= m; i++ {
      num := i
	  sum := 0
	  n := int64(math.Floor(math.Log10(float64(num))) + 1)
	  for ; num > 0; num /= 10 {
	     sum += int(math.Pow(float64(num%10), float64(n)))
	  }
	  if i == sum {
	     fmt.Printf("%d ", i)
	  }
   }
}

Output

Armstrong numbers between 100 and 1000 are:

153 370 371 407

Method 2: By Using Strconv Package

In this method, we will use strconv package in order to find the Armstrong numbers between two limits.

Syntax

func Itoa(x int) string

The Itoa() function is present in strconv package and is used to get the string representation of an integer variable when the base is 10. The function accepts the variable whose string equivalent is to be obtained as argument and returns the string representation which we can store and print on the screen.

func len(v Type) int

The len() function is used to get the length of a any parameter. It takes one parameter as the data type variable whose length we wish to find and returns the integer value which is the length of the variable.

func Atoi(s string) (int, error)

The Atoi() function is present in strconv package and is used to convert a string value in a given base to integer. The function takes a string variable as argument and returns the integer equivalent of that string along with an error variable. The error variable has a value if there is a problem in performing the respective conversion.

Algorithm

  • Step 1 − First, we need to import the fmt and strconv package.

  • Step 2 − Then, start the main() function. Inside the main() initialize two integer type variables to store the upper and lower limit of numbers

  • Step 3 − Print both these numbers

  • Step 4 − Use a for loop to iterate over each element present between these numbers.

  • Step 5 − Use Itoa() function of strconv package to convert the current number to string

  • Step 6 − find the length of that number by using len() function.

  • Step 7 − Now, call the pow() function to find the sum of digits of the numbers together by multiplying them with the 10place of the number

  • Step 8 − Store the value returned by the function in a variable.

Example

Go language program to display armstrong numbers between two intervals by using strconv package

package main
import (
   "fmt"
   "strconv"
)
func main() {
   var n int = 1000
   var m int = 10000
   fmt.Println("Armstrong numbers between", n, "and", m, "are:")
   fmt.Println()
   for i := n; i <= m; i++ {
      num := strconv.Itoa(i)
	   sum := 0
	   n := len(num)
      for _, digit := range num {
	      d, _ := strconv.Atoi(string(digit))
		   sum += pow(d, n)
	   }
      if i == sum {
         fmt.Printf("%d ", i)
	   }
   }
}
func pow(a, b int) int {
   res := 1
   for i := 0; i < b; i++ {
      res *= a
   }
   return res
}

Output

Armstrong numbers between 1000 and 10000 are:

1634 8208 9474

Conclusion

We have successfully compiled and executed a go language program to display Armstrong numbers between two intervals along with examples. We have taken two examples here. In the first example we are using functions present in math package in order to find narcissistic number while in the second example we are using strconv package to achieve the result.

Updated on: 13-Feb-2023

221 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements