Skip to content

Commit cda17c7

Browse files
authored
Merge pull request hacktoberfest17#732 from raaloottan/raalootan
shell factorial
2 parents 3fcb5cf + a681db0 commit cda17c7

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

factorial/shell/factorial.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
fact=1
3+
#taking input from user
4+
echo -e "Please enter a finite possitive number"
5+
read n
6+
#if enter value less than 0
7+
if [ $n -le 0 ] ; then
8+
echo "invalid number"
9+
exit
10+
fi
11+
#factorial logic
12+
if [ $n -gt 0 ] ; then
13+
for((i=$n;i>=1;i--))
14+
do
15+
fact=`expr $fact \* $i`
16+
done
17+
fi
18+
echo "The factorial of $n is $fact"

0 commit comments

Comments
 (0)