Pages

Tuesday, October 27, 2009

Shell Script to Find Factorial of a Number

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

18 comments:

  1. when i run method 2 in terminal. my terminal just closes without any result..

    ReplyDelete
  2. I think you are running script in current shell without arguments, I mean

    . fact

    after parameter testing "exit 1 " terminates your shell. Try to use following formats

    ./fact
    sh fact

    ReplyDelete
  3. thanx i got concept of recursion in bash shell script,which i tried to learn from to many places but in vain........

    ReplyDelete
  4. hi. i need shell program to print all the prime numbers between 1 to 100. can u give the coding? pls..

    ReplyDelete
  5. I already posted it here
    http://bashscript.blogspot.com/2009/11/shell-script-to-produce-prime-numbers.html

    ReplyDelete
  6. Great Job... Thanks for your valuable job.. i reached a right place...


    www.tipsinside.com

    ReplyDelete
  7. nice tips i can learn very well

    ReplyDelete
  8. good job yar and thank you....!

    ReplyDelete
  9. good job.... bharath

    ReplyDelete
  10. fact=`echo $fact \* $num|bc

    before * there is \ ..cany anybody give me the explanation regarding this..please

    ReplyDelete
  11. it is to remove the effect of special character *

    ReplyDelete
  12. can any one post the shell script to find the factorial of a given number using for loop

    ReplyDelete
  13. clear
    echo -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 "

    ReplyDelete
  14. There is no need to call bc more than once. An Optimization would be:

    #!/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 ...

    ReplyDelete
  15. But they want for loops....
    Would 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

    ReplyDelete
  16. i completely disappointed .......

    ReplyDelete