如果一个数的个位的三次方加十位的三次方加百位的三次等于他本身。则该数被判定为水仙花数。比如153=111+555+333.
//输出所有的三位水仙花数
#include<iostream>;
using namespace std;
int main() {
int num1 = 100;
do {
int a = 0, b = 0, c = 0;//个位、十位、百位
a = num1 % 10;
b = num1 / 10 % 10;
c = num1 / 100;
int M = a * a * a + b * b * b + c * c * c;
if (M == num1) {
cout << num1 << "是水仙花数" << endl;
}
num1++;
}
while (num1<1000);
system("pause");
return 0;
}
判断一个数是否为水仙花数。
//判断一个数是否为水仙花数
#include<iostream>;
using namespace std;
int main() {
int num1 = 0;
cout << "请输入一个数字:" << endl;
cin >> num1;
int a = 0, b = 0, c = 0;//个位、十位、百位
a = num1 % 10;
b = num1 / 10 % 10;
c = num1 / 100;
int M = a * a * a + b * b * b + c * c * c;
if (M == num1) {
cout << num1 << "是水仙花数" << endl;
}
else
{
cout << num1 << "不是水仙花数" << endl;
}
system("pause");
return 0;
}