C#认证考试试题汇编1_1

输入一个正整数n,若n为奇数,程序计算出数列1+3+5....+n;若n为偶数,则计算出2+4+6....+n之和

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
int x = int.Parse( Console.ReadLine());
int y = 0;
for (int i = x; i > 0; i -= 2)
{
y += i;
}
Console.WriteLine(y);
Console.Read();
}
}
}

posted @ 2017-09-18 20:57  刘振宇201612052458  阅读(140)  评论(0编辑  收藏  举报