用C#语言求出1到100之间奇数数字之和?

2024-12-04 14:10:08
推荐回答(3个)
回答1:

解答:(全部为手写,勿忘采纳啊!放入MAIN函数可直接运行)

     你创建一个控制台应用程序:

     static void Main(string[] args)

        {

            int sum=0;

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

            {

                if (i % 2 != 0)

                {

                    sum += i;

                }

            }

            Console.WriteLine("1到100之间奇数数字之和是{0}",sum);

       Console.ReadKey();

        } 

  (全部为手写,勿忘采纳啊!放入MAIN函数可直接运行)

回答2:

{
class Class1
{
public static int even()
{
int result=1;
int i=0;
while(i<=100)
{
result+=i;//result=result+i
i+=2;
}
Console.WriteLine("result:{0}",result);
return 0;
}
static void Main()
{
even();
}
}
}

回答3:

int Result = 0;
for (int i = 1; i <= 100; i = i + 2)
Result += i;
Console.WriteLine(Result);