用while语句,求1到100的偶数之和

2024-11-26 13:11:04
推荐回答(5个)
回答1:

public class Demo05ShiCao{

public static void main(String[] args){

int sum1 = 0;

for(int i = 1; i <= 100; i++){

if(i % 2 == 0){

sum1 +=i;

}

}

System.out.println("结果是:" + sum1);

System.out.println("==============================================");

int sum2 = 0;

int c = 1;

while(c <= 100){

c++;

if(c % 2 == 0){

sum2 += c;

}

}

System.out.println("结果是:"+sum2);

System.out.println("==============================================");

int sum3 = 0;

int d = 1;

do{

if(d % 2 == 0){

//int d;

sum3 += d;

}

d++;

}while(d <= 100);

System.out.println("结果是:"+sum3);

}

}

拿走不谢

回答2:

#include
main()
{
int i=0,sum=0;
while(i<=100)
{i+=2;
sum+=i;
}
printf("1到100的偶数之和为:%d",sum);
}

回答3:

wihle的做法
int sum = 0; //当前之和

int i = 1; //加数
while (i<=100){
if (i%2==0){//模0=偶数
sum+=i;//偶数进来后他们相加
}
i++;
}
System.out.println(sum);

回答4:

楼下的用的是C,楼主需要什么编程语言的啊?
vf的是:
store 0 to i,s
do while i<=100
if mod(i,2)=0
s=s+i
endif
i=i+1
enddo
?s
结果就是2550

回答5:

public class work13 {

public static void main(String[] args) {
// int sum=0;
// for (int i = 1; i <= 100; i++ ){
// if (i%2==0){
// sum = sum + i;
// }
// }
// System.out.println(sum);
int i = 0;
int sum = 0;
while(i <= 100){
sum = sum + i;
i =i + 2;
}
System.out.println(sum);
}
}