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

<div class="code-mirror  language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token number">153</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token operator">^</span><span class="token number">3</span> <span class="token operator">+</span> <span class="token number">5</span><span class="token operator">^</span><span class="token number">3</span> <span class="token operator">+</span> <span class="token number">3</span><span class="token operator">^</span><span class="token number">3</span>
<span class="token operator">   =</span> <span class="token number">1</span> <span class="token operator">+</span> <span class="token number">125</span> <span class="token operator">+</span> <span class="token number">27</span>
<span class="token operator">   =</span> <span class="token number">153</span>
</div>

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

<div class="code-mirror  language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">package</span> <span class="token namespace">main</span>

<span class="token comment">// fmt package provides the function to print anything</span>
<span class="token keyword">import</span> <span class="token string">"fmt"</span>

func <span class="token function">isArmstrongNumber</span><span class="token punctuation">(</span>num int32<span class="token punctuation">)</span> bool <span class="token punctuation">{</span>

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

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: 2022-08-29T06:39:34+05:30

365 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements