用for和do-while语句编写程序,求1到100的偶数和 ? 速求

2024-10-28 14:29:27
推荐回答(2个)
回答1:

int sum=0;
for (int i=1; i<=100; i++)
{
if (i-2*(i/2)==0)
sum=sum+i;
}
System.out.println("Sum="+sum);

//use do while
int i=1, sum=0;
do{
if (i-2*(i/2)==0)
sum=sum+i;
}while (i<=100)
System.out.println("Sum="+sum);

回答2:

#include
using namespace std;
int main()
{
int sum=0;
for(int i=2;i<=100;i+=2)
sum+=i;
cout< return 0;
}
#include
using namespace std;
int main()
{
int sum=0;
int i=2;
while(i<=100)
{
sum+=i;
i+=2;
}

cout< return 0;
}