Method 1:
#!/bin/bash
# fact1
# Finding factorial of a given number
#
echo "Enter a number"
read num
fact=1
n=$num
while [ $num -ge 1 ]
do
fact=`echo $fact \* $num|bc`
# You can use bellow commands also
# fact=$((fact*num))
# fact=`expr $fact \* $num`
# But maximum integer value that bash can handle is
# 9223372036854775807.
# An operation that takes a variable outside these
# limits will give an erroneous result. To solve this
# problem store numbers as strings and use bc for math.
let num--
done
echo "factorial of $n is $fact"
Method 2:
#!/bin/bash
# fact2
# Usage : sh fact2 Number
factorial ()
{
local number=$1
if [ $number -eq 0 ]
then
Factorial=1
else
let "next = number - 1"
factorial $next
Factorial=`echo $number \* $Factorial | bc`
# let "Factorial = $number * $Factorial"
fi
return $Factorial 2>/dev/null
}
# Bash function returns integer value only.
# But maximum integer value that bash can handle is
# 9223372036854775807.
# An operation that takes a variable outside these limits
# will give an erroneous result. That's why I redirected
# stderror output to /dev/null
# Main program starts here.
if [ $# -ne 1 ]
then
echo "Invalid Arguments"
echo "Usage: ./fact2.sh Number "
exit 1
fi
factorial $1
echo "Factorial of $1 is $Factorial"
Output:
[root@localhost shell]# ./fact1
Enter a number
8
factorial of 8 is 40320
[root@localhost shell]# ./fact1
Enter a number
23
factorial of 23 is 25852016738884976640000
[root@localhost shell]# ./fact2
Invalid Arguments
Usage: ./fact2.sh Number
[root@localhost shell]# ./fact2 7
Factorial of 7 is 5040
[root@localhost shell]# ./fact2 33
Factorial of 33 is 8683317618811886495518194401280000000
Tuesday, October 27, 2009
Posted by venu k
23 comments | 8:41 AM
Subscribe to:
Post Comments (Atom)
when i run method 2 in terminal. my terminal just closes without any result..
ReplyDeleteI think you are running script in current shell without arguments, I mean
ReplyDelete. fact
after parameter testing "exit 1 " terminates your shell. Try to use following formats
./fact
sh fact
Working Tx
ReplyDeleteini script buat apa??
ReplyDeletethanx i got concept of recursion in bash shell script,which i tried to learn from to many places but in vain........
ReplyDeletehi. i need shell program to print all the prime numbers between 1 to 100. can u give the coding? pls..
ReplyDeleteI already posted it here
ReplyDeletehttp://bashscript.blogspot.com/2009/11/shell-script-to-produce-prime-numbers.html
Great Job... Thanks for your valuable job.. i reached a right place...
ReplyDeletewww.tipsinside.com
nice tips i can learn very well
ReplyDeletegood job yar and thank you....!
ReplyDeletegood job.... bharath
ReplyDeletefact=`echo $fact \* $num|bc
ReplyDeletebefore * there is \ ..cany anybody give me the explanation regarding this..please
it is to remove the effect of special character *
ReplyDeletecan any one post the shell script to find the factorial of a given number using for loop
ReplyDeleteclear
ReplyDeleteecho -n " enter a no. "
read n
i=1
mul=1
until [ $i -gt $n ]
do
mul=`expr $mul \* $i `
i=`expr $i + 1 `
done
echo " factorial of $n is $mul "
There is no need to call bc more than once. An Optimization would be:
ReplyDelete#!/bin/bash
if [ $# -ne 1 ]
then
echo "Invalid Arguments"
echo "Usage: $(basename $0) Number"
exit 1
fi
seq -s'*' 1 "$1" | bc
# End of Script
There's no for loops or infinite calculations... Run as fast as ANSI C ...
But they want for loops....
ReplyDeleteWould be:
#!/bin/bash
# using one command line parameter
if [ $# -ne 1 ]
then
echo "Invalid Arguments"
echo "Usage: $(basename $0) Number"
exit 1
fi
echo "a=1;for (i=1;i<=$1;i++) a=a*i;a" | bc
# End of script
Also as fast as ANSI C
i completely disappointed .......
ReplyDeleteggood
ReplyDeleteHAVE A LOOK ON THIS
ReplyDeleteX=1
Y=1
echo "enter the number N"
READ N
WHILE [ $X -LE $N ]
DO
Y=`EXPR $Y \* $X`
X=`EXPR $X + 1`
DONE
ECHO " THE FACTORIAL OF $N IS $Y"
you guys are utterly useless..... i just want a bloody range and you won't give me it. Commit
ReplyDelete8888 u
ReplyDeleteHi There,
ReplyDeleteSmokin hot stuff! You’ve trimmed my dim. I feel as bright and fresh as your prolific website and blogs!
I'm new to the world of programming and I am really under pressure to learn python at the moment for a mature student college course. So im looking for help on how to get python pandas installed on ubuntu 16.04, specifically pandas datareader. I might need more help later too, to get my head around this. I have no programming experience and very little linux experience.
I'm doing the basic introduction on python and stuck on the second part.
import pandas_datareader.data as web
Thank you very much and will look for more postings from you.
Thanks a heaps,
Irene Hynes