编写shell脚本,输入n,求1-n的累加和
展开全部
#!/bin/bash
read -p "Input value of n: " n
sum=0
i=1
while (($i<=$n))
do
sum=$(($sum +$i))
i=$(($i+1))
done
echo "SUM is $sum."
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#!/bin/sh
if [ -z $1 ];then
echo Usage $0 num
exit 1
fi
sum=0
function ff_forsum(){
for num in $(seq $1)
do
sum=$(($sum+$num))
done
}
# call ff_forsum
ff_forsum $1
echo $sum
或者
#!/bin/sh
if [ -z $1 ];then
echo Usage $0 num
exit 1
fi
sum=0
function ff_whilesum(){
i=1
while(($i<=$1))
do
sum=$(($sum+$i))
i=$(($i+1))
done
}
# call ff_whilesum
ff_whilesum
echo $sum
或者
#!/bin/sh
if [ -z $1 ];then
echo Usage $0 num
exit 1
fi
sum=0
i=$1
function ff_whilesum(){
while(($i>0))
do
((sum=sum+i))
((i--))
done
}
# call ff_whilesum
ff_whilesum
echo $sum
if [ -z $1 ];then
echo Usage $0 num
exit 1
fi
sum=0
function ff_forsum(){
for num in $(seq $1)
do
sum=$(($sum+$num))
done
}
# call ff_forsum
ff_forsum $1
echo $sum
或者
#!/bin/sh
if [ -z $1 ];then
echo Usage $0 num
exit 1
fi
sum=0
function ff_whilesum(){
i=1
while(($i<=$1))
do
sum=$(($sum+$i))
i=$(($i+1))
done
}
# call ff_whilesum
ff_whilesum
echo $sum
或者
#!/bin/sh
if [ -z $1 ];then
echo Usage $0 num
exit 1
fi
sum=0
i=$1
function ff_whilesum(){
while(($i>0))
do
((sum=sum+i))
((i--))
done
}
# call ff_whilesum
ff_whilesum
echo $sum
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询