C# 练习

1、

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace _17._9._18
{

class Program
    {
        static void Main(string[] args)
        {
            Console.Write("请输入一个整数:");
            int n = int.Parse(Console.ReadLine());
            int sum=0;
            if(n%2!=0)
            {
                for(int i=1;i<=n;i++)
                {
                    if (i % 2 != 0)
                        sum += i;
                }
                Console.Write("该数及比该数小的所有奇数之和为:");
                Console.WriteLine(sum);
                Console.Read();
            }
            else
            {
                for (int j = 2; j <= n; j++)
                {
                    if (j % 2 == 0)
                        sum += j;
                }
                Console.Write("该数及比该数小的所有偶数之和为;");
                Console.WriteLine(sum);
                Console.Read();
               
            }
        }
    }
}

时间: 2024-10-07 04:38:12