c#入门递归求平方和

2008-04-10 15:55

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入最大数n");
            int n = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(sum(n));
        }
        private static int sum(int n)
        {
            int nsum;
            if (n == 1)
                 nsum = 1;
            else
            nsum = n * n + sum(n - 1);
        return
            nsum;
        }
    }
}

posted @ 2010-02-03 09:42  玄魂  阅读(951)  评论(0编辑  收藏  举报