linux shell编程计算1+2+3+4+5+..+100的结果

2024-12-03 05:47:15
推荐回答(4个)
回答1:

#!/bin/bash
i=1
sum=0
while [ $i -le100 ]
do
sum=`expr $i + $sum`
let i++
done
echo 1 and 100 equal $sum

#################################
#!/bin/bash
sum=0
for i in `seq 1 100`
do
sum=`expr $i + $sum`
done
echo 1 and 100 equal $sum

回答2:

#!/bin/sh
SUM=0
TMP=1
while [ $TMP -lt 100 ]
do
SUM=`expr $TMP + $SUM`
TMP=`expr $TMP + 1`
done
echo "$SUM"

回答3:

#! /bin/bash

sum=0
for num in {1..100}
do
((sum=$sum+$num))
done
echo "Total= $sum"

回答4:

dawdwd ad