Skip to content

Commit f4acf33

Browse files
authored
Create armstrong.sh
1 parent e8ee06d commit f4acf33

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

armstrong.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/bash
2+
count_digits() {
3+
local num=$1
4+
local count=0
5+
6+
while [ $num -gt 0 ]; do
7+
num=$((num / 10))
8+
count=$((count + 1))
9+
done
10+
11+
echo $count
12+
}
13+
is_armstrong() {
14+
local num=$1
15+
local sum=0
16+
local original_num=$num
17+
local num_digits=$(count_digits $num)
18+
19+
while [ $num -gt 0 ]; do
20+
digit=$((num % 10))
21+
sum=$((sum + digit ** num_digits))
22+
num=$((num / 10))
23+
done
24+
25+
if [ $sum -eq $original_num ]; then
26+
echo "true"
27+
else
28+
echo "false"
29+
fi
30+
}
31+
read -p "Enter a number: " user_input
32+
result=$(is_armstrong $user_input)
33+
34+
if [ $result == "true" ]; then
35+
echo "$user_input is an Armstrong number."
36+
else
37+
echo "$user_input is not an Armstrong number."
38+
fi

0 commit comments

Comments
 (0)