//1、使用do-while
int sum = 0; //当前之和
int i = 1; //加数
do {
if (i%2==0){
sum+=i;
}
i++;
}while (i<=100);
System.out.println(sum);
------------------------------------------
//2、使用wihle的做法:
int sum = 0; //当前之和
int i = 1; //加数
while (i<=100){
if (i%2==0){//模0=偶数
sum+=i;//偶数进来后他们相加
}
i++;
}
System.out.println(sum);
---------------------------
//3、使用for做法
int sum = 0; //当前之和
for (int i = 0; i <=100; i++) {
if (i%2==0){
sum+=i;
}
}
System.out.println(sum);
#include
int main()
{
int sum = 0, i = 1;
while (i <= 100){
if (i % 2 == 0)sum += i;
i++;
}
printf ("%d\n", sum);
return 0;
}
java中
public class p39 {
public static void main(String[] args) {
int s=0;
int i=2;
while(i<=100) {
s=s+i;
i=i+2;
}
System.out.println("s="+s);
}
}
C语言中
#include
main()
{ int i=2,s=0;
while(i<=100)
{
s=s+i;
i=i+2;
}
printf("%d\n",s);
}