在linux上,用shell脚本实现1+2+3+……100,并在终端上显示出来

2024-12-17 02:11:24
推荐回答(3个)
回答1:

s=0
for (( i=1;i<=100;i=i+1 ))
do
s=$(($s+$i))
done
echo "1+2+3+......+100 ==>$s"

回答2:

#!/bin/sh
sum=0
for ((i=0;i<=100;i++))
do
sum=$((sum+i))
done
echo sum=$sum

回答3:

#!/usr/bin/perl
$counter += $_ for 1..100;
print $counter;