03 December 2012

Shell Script to find factorial of a number using Recursion

2 comments

Q. How do I find factorial of a number using recursion function in Bash


Ans:

#!/bin/bash
#Recursive factorial function
factorial()
{
local=$1
if((local<=2)); then
echo $local
else
f=$((local -1))
#Recursive call
f=$(factorial $f)
f=$((f*local))
echo $f
fi
}
#main script
read -p "Enter the number:" n
if((n==0)); then
echo 1
else
#calling factorial function
factorial $n
fi

Output


how to find factorial of a number using recursion


Enjoy :)


If You Liked This Post Please Take a Time To Share This Post

You May Also Like...

2 comments:

  1. Hi, thanks for your great information! Well, it really seems so great. http://www.lowhosting.com/networksolutions-review/

    ReplyDelete
  2. Worked perfectly first try, appreciate this post! This Spacebar Clicker profile made things fun to try. Clicking rapidly can be more fun than it sounds. Spacebar Clicker proves that upgrades and progress are enough to make a great game.

    ReplyDelete