#! /bin/bash
# using recursion
function factorial()
{
if [ $1 -eq 1 ]; then
echo 1
else
local temp=$[ $1 -1 ]
local result=`factorial $temp`
echo $[ $result * $1]
fi
}
read -p "Enter value:" value
result=`factorial $value`
echo "The factorial of $value is:$result"
阶乘循环--函数定义、函数调用
时间: 2024-10-12 09:28:52