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

// Golang Program to check Armstrong Number package main // fmt package provides the function to print anything import "fmt" func main() { fmt.Println("Number = 153") // declare the variables var number, temp, remainder int var result int = 0 // initialize the variables number = 153 temp = number // Use of For Loop for { remainder = temp % 10 result += remainder * remainder * remainder temp /= 10 if temp == 0 { break // Break Statement used to stop the loop } } // If satisfies Armstrong condition if result == number { fmt.Printf("%d is an Armstrong number.", number) } else { fmt.Printf("%d is not an Armstrong number.", number) } // print the result }

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

package main // fmt package provides the function to print anything import "fmt" // CREATE A FUNCTION TO CHECK FOR ARMSTRONG NUMBER func CHECKARMSTRONG(number int) bool { // declare the variables fmt.Printf("\nEntered Number =%d\n", number) temp := 0 remainder := 0 var result int = 0 // initialize the variables temp = number // Use of For Loop // here we calculate the sum of the cube of each digit of the // given number to check the given number is Armstrong or not for { remainder = temp % 10 result += remainder * remainder * remainder temp /= 10 if temp == 0 { break // Break Statement used to stop the loop } } // If satisfies Armstrong condition if result == number { fmt.Printf("%d is an Armstrong number.", number) } else { fmt.Printf("%d is not an Armstrong number.", number) } return true // print the result } func main() { fmt.Println("GOLANG PROGRAM TO CHECK ARMSTRONG NUMBER") // calling the function CHECKARMSTRONG() CHECKARMSTRONG(153) CHECKARMSTRONG(351) }

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: 15-Nov-2022

550 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements