求個個數字的平方和--C#

//編程序功能是輸入一個長整數,求各位數字的平方和s。如果輸入的是負數,求其相反數的各位數字的平方和。
//例如:輸入-123,輸出14。(用while或do-while語句實現)

using System;

namespace 各個數字和
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("請輸入一個數字:");
            int x =Math.Abs( Convert.ToInt32( Console.ReadLine ()));
            int y = 0,x2=x;
            while(x>0)
            {
                x2 = x % 10;
                y = y + x2 * x2;
                x = x / 10;
            }
            Console.WriteLine("你得到的數字和是:{0}",y);
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章