輸入一個正整數n,求1+2+3+......+n的和。spa
using System; namespace _2求1到n的和 { class Program { public static int Sum(int n) { int sum = 0; for (int i = 1; i <= n; i++) { sum += i; } return sum; } static void Main(string[] args) { int num = int.Parse(Console.ReadLine()); Console.WriteLine(Sum(num)); Console.ReadKey(); } } }