Golang Program to Check For Armstrong Number

In this tutorial, we will learn how to check for Armstrong number in Go programming language.

An Armstrong number or the narcissist number is a number whose sum of digits raised to the power three equals the number itself.

  • Example1: $\mathrm{(1^3) \:+ \:(5^3) \:+ \:(3^3)\:= \:153}$

  • Example2: $\mathrm{(3^3) \:+ \:(7^3) \:+ \:(1^3)\:= \:371}$

In the below examples, we will read an integer number and then check the given number is an Armstrong number using Golang program

Syntax

Syntax of For loop
Initialize
for condition {
}
incrementor

The initialization statement is optional and executes before for loop starts.

The condition statement holds a Boolean expression, which is evaluated at the starting of each iteration of the loop. If the value of the conditional statement is true, then the loop executes.

The incrementor statement is executed after the body of the for-loop. After the incrementor statement, the condition statement evaluates again if the value of the conditional statement is false, then the loop ends.

Example 1: The Following is an Example in which we Check for Armstrong Number using Function Within the Main Function

Algorithm

  • Step 1 ? Import the package fmt.

  • Step 2 ? Start function main ().

  • Step 3 ? Declare and initialize the variables.

  • Step 4 ? Use of for loop with condition and incrementor.

  • Step 5 ? Print the result using fmt.Printf().

Example

<div class="execute"></div><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 comment">// Golang Program to check Armstrong Number</span>
<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">main</span><span class="token punctuation">(</span><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><span class="token string">"Number = 153"</span><span class="token punctuation">)</span>
   <span class="token comment">// declare the variables</span>
   <span class="token keyword">var</span> number<span class="token punctuation">,</span> temp<span class="token punctuation">,</span> remainder <span class="token keyword">int</span>
   <span class="token keyword">var</span> result <span class="token keyword">int</span> <span class="token operator">=</span> <span class="token number">0</span>
   <span class="token comment">// initialize the variables</span>
   number <span class="token operator">=</span> <span class="token number">153</span>
   temp <span class="token operator">=</span> number
   <span class="token comment">// Use of For Loop</span>
   <span class="token keyword">for</span> <span class="token punctuation">{</span>
      remainder <span class="token operator">=</span> temp <span class="token operator">%</span> <span class="token number">10</span>
      result <span class="token operator">+=</span> remainder <span class="token operator">*</span> remainder <span class="token operator">*</span> remainder
      temp <span class="token operator">/=</span> <span class="token number">10</span>
      <span class="token keyword">if</span> temp <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">{</span>
         <span class="token keyword">break</span> <span class="token comment">// Break Statement used to stop the loop</span>
      <span class="token punctuation">}</span>
   <span class="token punctuation">}</span>
   <span class="token comment">// If satisfies Armstrong condition</span>
   <span class="token keyword">if</span> result <span class="token operator">==</span> number <span class="token punctuation">{</span>
      <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Printf</span><span class="token punctuation">(</span><span class="token string">"%d is an Armstrong number."</span><span class="token punctuation">,</span> number<span class="token punctuation">)</span>
   <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Printf</span><span class="token punctuation">(</span><span class="token string">"%d is not an Armstrong number."</span><span class="token punctuation">,</span> number<span class="token punctuation">)</span>
   <span class="token punctuation">}</span>
   <span class="token comment">// print the result</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

Number = 153
153 is an Armstrong number.

Description of the Code

  • In the above program, we first declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file.

  • We imported the fmt package that includes the files of package fmt then we can use a function related to the fmt package.

  • Now start the function main () and this function is the entry point of the executable programs. It does not take any argument nor return anything.

  • First, we declare the four integer variables. Initialize the variable number to an integer value you want and result variable to the 0.

  • Using for loop ? The condition is given inside an if statement and stop execution is mentioned by a break statement.

  • And last printing the result on the screen using fmt.Printf.

Example 2: The Following Is an Example in which we Check for Armstrong Number using two Separate Functions

Algorithm

  • Step 1 ? Import the package fmt.

  • Step 2 ? Create the CHECKARMSTRONG () function.

  • Step 3 ? Declare and initialize the variables.

  • Step 4 ? Use of for loop with condition and incrementor.

  • Step 5 ? Start function main ().

  • Step 6 ? Call the function CHECKARMSTRONG ().

  • Step 7 ? Print the result using fmt.Printf().

Example

<div class="execute"></div><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>

<span class="token comment">// CREATE A FUNCTION TO CHECK FOR ARMSTRONG NUMBER</span>
func <span class="token function">CHECKARMSTRONG</span><span class="token punctuation">(</span>number <span class="token keyword">int</span><span class="token punctuation">)</span> bool <span class="token punctuation">{</span>

   <span class="token comment">// declare the variables</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Printf</span><span class="token punctuation">(</span><span class="token string">"\nEntered Number =%d\n"</span><span class="token punctuation">,</span> number<span class="token punctuation">)</span>
   temp <span class="token operator">:</span><span class="token operator">=</span> <span class="token number">0</span>
   remainder <span class="token operator">:</span><span class="token operator">=</span> <span class="token number">0</span>
   <span class="token keyword">var</span> result <span class="token keyword">int</span> <span class="token operator">=</span> <span class="token number">0</span>
   
   <span class="token comment">// initialize the variables</span>
   temp <span class="token operator">=</span> number
   
   <span class="token comment">// Use of For Loop</span>
   <span class="token comment">// here we calculate the sum of the cube of each digit of the</span>
   <span class="token comment">// given number to check the given number is Armstrong or not</span>
   <span class="token keyword">for</span> <span class="token punctuation">{</span>
      remainder <span class="token operator">=</span> temp <span class="token operator">%</span> <span class="token number">10</span>
      result <span class="token operator">+=</span> remainder <span class="token operator">*</span> remainder <span class="token operator">*</span> remainder
      temp <span class="token operator">/=</span> <span class="token number">10</span>
      <span class="token keyword">if</span> temp <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">{</span>
         <span class="token keyword">break</span> <span class="token comment">// Break Statement used to stop the loop</span>
      <span class="token punctuation">}</span>
   <span class="token punctuation">}</span>
   <span class="token comment">// If satisfies Armstrong condition</span>
   <span class="token keyword">if</span> result <span class="token operator">==</span> number <span class="token punctuation">{</span>
      <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Printf</span><span class="token punctuation">(</span><span class="token string">"%d is an Armstrong number."</span><span class="token punctuation">,</span> number<span class="token punctuation">)</span>
   <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Printf</span><span class="token punctuation">(</span><span class="token string">"%d is not an Armstrong number."</span><span class="token punctuation">,</span> number<span class="token punctuation">)</span>
   <span class="token punctuation">}</span>
   <span class="token keyword">return</span> <span class="token boolean">true</span>
   <span class="token comment">// print the result</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 class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"GOLANG PROGRAM TO CHECK ARMSTRONG NUMBER"</span><span class="token punctuation">)</span>
   <span class="token comment">// calling the function CHECKARMSTRONG()</span>
   <span class="token function">CHECKARMSTRONG</span><span class="token punctuation">(</span><span class="token number">153</span><span class="token punctuation">)</span>
   <span class="token function">CHECKARMSTRONG</span><span class="token punctuation">(</span><span class="token number">351</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

GOLANG PROGRAM TO CHECK ARMSTRONG NUMBER

Entered Number =153
153 is an Armstrong number.
Entered Number =351
351 is not an Armstrong number.
Program exited.

Description of the Code

  • In the above program, we first declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file.

  • We imported the fmt package that includes the files of package fmt then we can use a function related to the fmt package.

  • Next create the CHECKARMSTRONG () function to check if the number is Armstrong number or not

  • Declare the integer variables temp, remainder and result to 0

  • Next, we use for loop with condition and incrementor to check for the condition of the number to be an Armstrong number or not. In the code, we calculated the sum of the cube of each digit of the given number to check the given number is Armstrong or not

  • Now we start the function main () and this function is the entry point of the executable programs. It does not take any argument nor return anything.

  • Next, we call the CHECKARMSTRONG () function by using a integer value which has to be checked for armstrong number

  • Finally the result is printed on the screen using fmt.Printf() function.

Conclusion

We have successfully compiled and executed the Golang program code to check for Armstrong number in the above two examples.

Updated on: 2022-11-15T12:21:48+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements