

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a given number divides the sum of the factorials of its digits in C++
Suppose, we have an integer, we have to find if the number divides the sum of the factorial of its digits. Suppose a number is 19, the sum of factorial of digits is (1! + 9!) = 362881, this is divisible by 19.
To solve this, we will take the number, then calculate factorial of each digit and add the sum, if the sum is divisible by the number itself, then return true, otherwise false.
Example
#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."; }
Output
Yes, the number can divides the sum of factorial of digits.
- Related Questions & Answers
- C Program to check if a number is divisible by sum of its digits
- C++ Program to Sum the digits of a given number
- Find the Largest number with given number of digits and sum of digits in C++
- C Program to check if a number is divisible by any of its digits
- Find last two digits of sum of N factorials using C++.
- Find smallest number with given number of digits and sum of digits in C++
- C Program to sum the digits of a given number in single statement
- Program to find the sum of all digits of given number in Python
- Check if a number is magic (Recursive sum of digits is 1) in C++
- Check if the frequency of all the digits in a number is same in Python
- Recursive sum all the digits of a number JavaScript
- Number of digits in the nth number made of given four digits in C++
- Write a Golang program to find the sum of digits for a given number
- Destructively Sum all the digits of a number in JavaScript
- Maximize the given number by replacing a segment of digits with the alternate digits given in C++
Advertisements