-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprog.sh
More file actions
80 lines (67 loc) · 1.24 KB
/
prog.sh
File metadata and controls
80 lines (67 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/bash
is_prime() {
num=$1
if (( num <= 1 )); then
echo "$num is not prime."
return
fi
for ((i = 2; i * i <= num; i++)); do
if (( num % i == 0 )); then
echo "$num is not prime."
return
fi
done
echo "$num is prime."
is_armstrong() {
num=$1
sum=0
temp=$num
num_digits=$(echo -n "$num" | wc -c)
while (( temp > 0 )); do
digit=$(( temp % 10 ))
sum=$(( sum + digit ** num_digits ))
temp=$(( temp / 10 ))
done
if (( sum == num )); then
echo "$num is an Armstrong number."
else
echo "$num is not an Armstrong number."
fi
}
}
is_pyramid_number() {
echo "Enter the number of rows:"
read rows
for i in $(seq 1 $rows)
do
for j in $(seq 1 $i)
do
echo -n "* "
done
echo
done
}
# Main Menu to choose which function to run
echo "Choose an option:"
echo "1. Check if a number is Prime"
echo "2. Check if a number is Armstrong"
echo "3. Make a pyramid"
read choice
# Take user input for the number
echo "Enter the number:"
read number
# Execute based on the user's choice
case $choice in
1)
is_prime $number
;;
2)
is_armstrong $number
;;
3)
is_pyramid_number $number
;;
*)
echo "Invalid option!"
;;
esac