用C#语言编写:输出1-100之间偶数和奇数的和

2024-12-04 20:55:19
推荐回答(2个)
回答1:

static void Main(){
int sum_odd, sum_even;
sum_odd=0;
sum_even=0;
for(int i=1;i<=100;i++){
if(i%2==0)sum_even+=i;
else sum_odd+=i;
}
Console.WriteLine("偶数之和="+sum_even.ToString());
Console.WriteLine("奇数之和="+sum_odd.ToString());
}

回答2:

int a,b;
a=0;
b=0;
for(int i=1;i<=100;i++){
if(i%2==0)a+=i;
a+=i;
}
Console.WriteLine("偶数之和="+a);
Console.WriteLine("奇数之和="+b);
Console.ReadLine();