How to Check Armstrong Number between Two Integers in Golang?


In this tutorial, we are going to write and explain the code for finding the Armstrong number between two integers. The Armstrong number is a number whose sum of the cube of all the digits in the number is equal to the number itself.

For example, 153 is a number as shown below

153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153

Algorithm

  • STEP 1 − First we are declaring the numbers between which we have to find the Armstrong Numbers.

  • STEP 2 − Now, we are taking input from the user for the numbers between which we have to find the Armstrong Numbers.

  • STEP 3 − Running the for loop from the first number to the last number and calling the function to check that the current number is Armstrong number or not and if yes then printing the number.

Example

Time Complexity:

O(1) - The time complexity is constant because no matter what is the input the program will take sane time.

Space Complexity:

O(1) - The variables are static in the program so the space complexity is also constant.

Code

package main // fmt package provides the function to print anything import "fmt" func isArmstrongNumber(num int32) bool { // declaring the sum variable which will store // the sum of the cube of each digit in the number var sum int32 = 0 // declaring and initializing the tempNum variable on which we will // perform some arithmetic operations ahead var tempNum int32 = num // running a for loop till the tempNum become zero for tempNum != 0 { // picking each digit by doing mode on the current number currDigit := tempNum % 10 // adding the cube of the current digit into the number sum = sum + (currDigit * currDigit * currDigit) // eliminating the last digit from the end tempNum = tempNum / 10 } // if the sum is equal to the number then returning true if sum == num { return true } return false } func main() { // declaring the integer number using the var keyword between which we // have to find the Armstrong numbers var number1, number2 int32 fmt.Println("Enter the numbers between which you want to find the Armstrong numbers.") // Taking the input of the integers from the user between which we // have to find the Armstrong numbers fmt.Println("Enter the first number:") fmt.Scanln(&number1) fmt.Println("Enter the second number:") fmt.Scanln(&number2) fmt.Println("The Armstrong number between", number1, "and", number2, "are as follow:") // In this for loop where we are passing each number between the two numbers we have // took from the user for num := number1; num <= number2; num++ { // here we are calling the function to check that the current number is Armstrong // number or not if isArmstrongNumber(num) { fmt.Println(num) } } }

Output:

Enter the numbers between which you want to find the Armstrong numbers.
Enter the first number:
0
Enter the second number:
10000
The Armstrong number between 0 and 10000 are as follow:
0
1
153
370
371
407

Description of code:

  • var number1, number2 int32 - This line of code is declaring two int32 variables. Between them, we have to find all the Armstrong numbers.

  • fmt.Scanln(<number1) and fmt.Scanln(<number2)- Here we are taking the input from the user.

  • for num := number1; num <= number2; num++ {} - This for is running from number1 till number2.

  • if isArmstrongNumber(num) {} - In this if condition the isArmstrongNumber() function is called and passes the parameter num which is the current index of the for loop. The if condition is checking the value return by isArmstrongNumber() function. If the value is true then we are printing the number.

  • func isArmstrongNumber(num int32) bool {} - This is the isArmstrongNumber() function. It consists of a num parameter with data type int32 and has a return type bool.

    • var sum int32 = 0 - Here we are declaring the sum variable of int32 type which will store the sum of the cube of each digit in the number.

    • var tempNum int32 = num - Declaring the tempNum variable of int32 type which is getting initialized by the num value. We will do arithmetic operations on tempNum that is why we are not directly performing these arithmetic operations on num variables which will get compared with sum later.

    • for tempNum != 0 {} - This for loop is running till the tempNum become zero.

    • currDigit := tempNum % 10 - We fetch the last digit of the current number by applying the % with 10 and storing that in currDigit.

    • sum = sum + (currDigit * currDigit * currDigit) - Adding the cube of currDigit to the sum variable.

    • tempNum = tempNum / 10 - Dividing the tempNum by 10 so that the last digit gets removed from the value.

    • if sum == num {} - In the end, we are comparing the sum with the number and returning true or else false.

Logic Explanation:

The Number is Armstrong

Suppose we have a number 371 and we have to check if that number is Armstrong or not.

  • Taking the last number by doing % - 371 % 10 = 1

    Sum = 0 + (1*1*1) -> sum = 1

    Num -> 371 /10 = 37

  • Taking the last number by doing % - 37 % 10 = 7

    Sum = 1 + (7*7*7) -> sum = 344

    Num -> 37 /10 = 3

  • Taking the last number by doing % - 3 % 10 = 3

    Sum = 344 + (3*3*3) -> sum = 371

    Num -> 3 /10 = 0

As you can see the sum is equal to the initial number that is why 371 is an Armstrong number.

Number is not Armstrong

Suppose we have a number 251 and we have to check if that number is Armstrong or not.

  • Taking the last number by doing % - 251 % 10 = 1

    Sum = 0 + (1*1*1) -> sum = 1

    Num -> 251 /10 = 25

  • Taking the last number by doing % - 25 % 10 = 5

    Sum = 1 + (5*5*5) -> sum = 126

    Num -> 25 /10 = 2

  • Taking the last number by doing % - 2 % 10 = 2

    Sum = 126 + (2*2*2) -> sum = 134

    Num -> 2 /10 = 0

Conclusion

This is all about the Golang code to find the Armstrong numbers between two numbers. To learn more about go you can explore these tutorials.

This is all about the Golang code to find the Armstrong numbers between two numbers. To learn more about go you can explore these tutorials.

Updated on: 29-Aug-2022

191 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements