Program to check if a number is an armstrong number.

A positive integer is called an Armstrong number if the sum of cubes of individual digit is equal to that number itself. For example:

153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.
12 is not equal to 1*1*1+2*2*2 // 12 is not an Armstrong number.

Go program :
package main

import “fmt”

func main() {
var a int
var num int
fmt.Println(“Program to find if the number is an armstrong number.\nEnter a number:”)
fmt.Scanf(“%d”, &a)
n := a //storing the value of a in another variable
for {
if n != 0 {
rem := n % 10 //rem is for the remainder. % is used for modulus of a number. It returns the last digit
num += rem * rem * rem //multiplying the remainder thrice and then adding
n = n / 10 //reduce n to 2 digits
} else {
break
}
}
if num == a {
fmt.Println(“It is an armstong number”)
} else {
fmt.Println(“It is not an armstrong number”)
}
}

Output :

prince@prince-Aspire-V3-571:~/files/goprog$ go run checkarmstrong.go
Program to find if the number is an armstrong number.
Enter a number:
153
It is an armstong number
prince@prince-Aspire-V3-571:~/files/goprog$ go run checkarmstrong.go
Program to find if the number is an armstrong number.
Enter a number:
12
It is not an armstrong number

One thought on “Program to check if a number is an armstrong number.

  1. Nice to see you writing blogs Atul. Great to see that you are developing that habit. Current blog post could be simple right now but in future this habit will help you to write good technical contents. Keep it up.

    Like

Leave a comment