Skip to content

Commit 57a20cf

Browse files
kavithajmgithub-action
and
github-action
authored
Adding program to check if the number is an Armstrong number (#442)
* Adding program to check if the number is an armstrong number * Updated Documentation in README.md * Update loop in isarmstrong Co-authored-by: github-action <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 9fec515 commit 57a20cf

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.
4646
1. [`Result`](./strings/ahocorasick/ahocorasick.go#L9): No description provided.
4747

4848

49+
---
50+
</details><details>
51+
<summary> <strong> armstrong </strong> </summary>
52+
53+
---
54+
55+
##### Functions:
56+
57+
1. [`IsArmstrong`](./math/armstrong/isarmstrong.go#L14): No description provided.
58+
4959
---
5060
</details><details>
5161
<summary> <strong> avl </strong> </summary>

math/armstrong/isarmstrong.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// isarmstrong.go
2+
// description: Checks if the given number is armstrong number or not
3+
// details: An Armstrong number is a n-digit number that is equal to the sum of each of its digits taken to the nth power.
4+
// ref: https://mathlair.allfunandgames.ca/armstrong.php
5+
// author: Kavitha J
6+
7+
package armstrong
8+
9+
import (
10+
"math"
11+
"strconv"
12+
)
13+
14+
func IsArmstrong(number int) bool {
15+
var rightMost int
16+
var sum int = 0
17+
var tempNum int = number
18+
19+
// to get the number of digits in the number
20+
length := float64(len(strconv.Itoa(number)))
21+
22+
// get the right most digit and break the loop once all digits are iterated
23+
for tempNum > 0 {
24+
rightMost = tempNum % 10
25+
sum += int(math.Pow(float64(rightMost), length))
26+
27+
// update the input digit minus the processed rightMost
28+
tempNum /= 10
29+
}
30+
31+
return number == sum
32+
}

math/armstrong/isarmstrong_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// isarmstrong_test.go
2+
3+
package armstrong
4+
5+
import "testing"
6+
7+
var testCases = []struct {
8+
name string // test description
9+
input int // user input
10+
expected bool // expected return
11+
}{
12+
{
13+
"negative number: Not an armstrong number",
14+
-140,
15+
false,
16+
},
17+
{
18+
"positive number: Not an armstrong number",
19+
23,
20+
false,
21+
},
22+
{
23+
"smallest armstrong number",
24+
0,
25+
true,
26+
},
27+
{
28+
"smallest armstrong number with more than one digit",
29+
153,
30+
true,
31+
},
32+
{
33+
"random armstrong number",
34+
407,
35+
true,
36+
},
37+
{
38+
"random armstrong number",
39+
9474,
40+
true,
41+
},
42+
}
43+
44+
func TestIsArmstrong(t *testing.T) {
45+
for _, test := range testCases {
46+
t.Run(test.name, func(t *testing.T) {
47+
funcResult := IsArmstrong(test.input)
48+
if test.expected != funcResult {
49+
t.Errorf("Expected answer '%t' for the number '%d' but answer given was %t", test.expected, test.input, funcResult)
50+
}
51+
})
52+
}
53+
}

0 commit comments

Comments
 (0)