检查给定数字是否除以C ++中其数字的阶乘和
假设我们有一个整数,我们必须确定数字是否除以其数字的阶乘之和。假设数字为19,则阶乘之和为(1!+9!)=362881,可以除以19。
为了解决这个问题,我们将取数字,然后计算每个数字的阶乘并加和,如果和数可被数字本身整除,则返回true,否则返回false。
示例
#include <iostream>
using namespace std;
int factorial(int n){
if(n == 1 || n == 0)
return 1;
return factorial(n - 1) * n;
}
bool isDigitsFactDivByNumber(int num){
int temp = num;
int sum = 0;
while(num){
int digit = num % 10;
sum += factorial(digit);
num /= 10;
}if(sum%temp == 0){
return true;
} return false;
}
int main() {
int number = 19;
if (isDigitsFactDivByNumber(number))
cout << "Yes, the number can divides the sum of factorial of digits.";
else
cout << "No, the number can not divides the sum of factorial of digits.";
}输出结果
Yes, the number can divides the sum of factorial of digits.
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短