C#求水仙花数

水仙花数是指各位数的三次方之和等于它本身,所有要先提取出各位数

 //求100-1000的水仙花数
            Console.WriteLine("100-1000之间的水仙花数:");
            int num = 100;
            while(num < 1000)
            {
                int s;//个位数
                int t;//十位数
                int h;//百位数
                //提取出各位数
                s = num % 10;
                t = num / 10 % 10;
                h = num / 100 ;
                if(num == s * s* s + t * t * t + h * h * h)
                //可改为 if(Math.Pow(s,3) + Math.Pow(t,3) + Math.Pow(h,3))
                {
                    Console.WriteLine("水仙花数是" + num);
                }
                num++;
            }

你可能感兴趣的:(C#求水仙花数)