do…while-水仙花数
练习案例:水仙花数
案例描述:水仙花数是指一个3位数,它的每个位上的数字的3次幂之和等于它本身
例如: 1^3 + 5^3 + 3^3 = 153
请利用do…while语句,求出所有3位数中的水仙花数
#include<iostream>
using namespace std;
int main()
{
int num = 100;//最小的三位数
do
{
int a = 0;
int b = 0;
int c = 0;
a = num % 10;//取余于10,求个位数
b = (num / 10) % 10;//三位数除以10得前两位,再取余于10得十位数
c = num / 100;//三位数除以100得百位数
if ((a * a * a + b * b * b + c * c * c) == num)
{
cout << num << "为水仙花数" << endl;
}
num++;
} while (num<999);
system("pause");
return 0;
}