技术改变生活

博客园 首页 新随笔 联系 订阅 管理

阶乘计算

  版本一:

[root@centos17 shell]# sh -v factorial.sh 
#!/bin/bash
factorial() {
factorial1=1
for((i=1;i<=5;i++));do
	factorial1=$((factorial1*i))
done
echo "5的阶乘是:$factorial1"
}

factorial
5的阶乘是:120
[root@centos17 shell]# 

  版本二:

[root@centos17 shell]# sh -v factorial.sh
#!/bin/bash
factorial() {
factorial1=1
for((i=1;i<=$num;i++));do
	factorial1=$((factorial1*i))
done
echo "$num的阶乘是:$factorial1"
}
num=5
factorial
5的阶乘是:120
[root@centos17 shell]# 

  版本三:

[root@centos17 shell]# sh -v factorial.sh 5
#!/bin/bash
factorial() {
factorial1=1
for((i=1;i<=$1;i++));do
	factorial1=$((factorial1*i))
done
echo "$1的阶乘是:$factorial1"
}
factorial $1
5的阶乘是:120
[root@centos17 shell]# 

  版本四:

[root@centos17 shell]# sh -v factorial.sh 5 8 10
#!/bin/bash
factorial() {
factorial1=1
for((i=1;i<=$1;i++));do
	factorial1=$((factorial1*i))
done
echo "$1的阶乘是:$factorial1"
}
factorial $1
5的阶乘是:120
factorial $2
8的阶乘是:40320
factorial $3
10的阶乘是:3628800
[root@centos17 shell]# 

  

posted on 2020-10-05 19:28  小阿峰  阅读(147)  评论(0编辑  收藏  举报