php程序,使用while循环语句求1到100中(如1+3+5…)所有奇数累加的值

另外用for或do....while循环语句编写都可以
2025-05-07 23:05:34
推荐回答(4个)
回答1:

$num=0;
for($i=1;$i<=100;$i+=2){
$num+=$i;
}
echo $num;
?>

回答2:

$c=0;
for($i=1;$i<=100;$i++)
{
if($i%2!=0)
{
$c+=$i;
}
}
echo $c;

回答3:

$i=0;
$num=0;
while($i<=100){
if($i%2==1) $num+=$i;
$i++;
}
echo $num;
?>

回答4:

$i=0;
$num=0;
while($i<=100){
if($i%2==1)
$num+=$i;
$i++;
}
echo
$num;
?>