Write a C++ Program to find the Strong Number with an example. Any number can be strong if the sum of the factorial of individual digits in that number equal to the actual number. For instance, factorial of the individual digits in 145 = 1! + 4! + 5! = 1 + 24 + 120 = 145. So, 145 is a perfect number.
In this C++ strong number example, we used reminder = temp % 10 to get the last digit of a number. Next, we are finding the factorial of that number using for loop. Finally, we used the If condition to check whether the number equal to the sum of factorials and print the result.
#include<iostream> using namespace std; int main() { int i, number, temp, reminder; long sum = 0, factorial = 1; cout << "\nPlease Enter the Number to Check for Strong Number = "; cin >> number; for(temp = number; temp > 0; temp = temp / 10 ) { factorial = 1; reminder = temp % 10; for (i = 1; i <= reminder; i++) { factorial = factorial * i; } cout << "\nThe Factorial of "<< reminder << " = " << factorial; sum = sum + factorial; } cout << "\n\nThe Sum of the Factorials of " << number << " is = " << sum << "\n\n"; if(number == sum) { cout << number << " is a Strong Number"; } else { cout << number << " is Not a Strong Number"; } return 0; }
C++ Program to find the Strong Number using a While loop
#include<iostream> using namespace std; int main() { int i, number, temp, reminder; long sum = 0, factorial = 1; cout << "\nPlease Enter the Number to Check for Strong Number = "; cin >> number; temp = number; while(temp > 0) { factorial = 1; i = 1; reminder = temp % 10; while (i <= reminder) { factorial = factorial * i; i++; } cout << "\nThe Factorial of "<< reminder << " = " << factorial; sum = sum + factorial; temp = temp / 10; } cout << "\nThe Sum of the Factorials of " << number << " is = " << sum << "\n\n"; if(number == sum) { cout << number << " is a Strong Number"; } else { cout << number << " is Not a Strong Number"; } return 0; }
Please Enter the Number to Check for Strong Number = 127
The Factorial of 7 = 5040
The Factorial of 2 = 2
The Factorial of 1 = 1
The Sum of the Factorials of 127 is = 5043
127 is Not a Strong Number
C++ Program to find the Strong Number using Recursive Functions
In this C++ example, the long factorialOfNumber(int number) method finds the factorial of a number.
#include<iostream> using namespace std; long factorialOfNumber(int number) { if (number == 0 || number == 1) return 1; else return number * factorialOfNumber(number - 1); } int main() { int number, temp, reminder; long sum = 0, factorial = 1; cout << "\nPlease Enter the Number to Check for Strong Number = "; cin >> number; for(temp = number; temp > 0; temp = temp / 10 ) { factorial = 1; reminder = temp % 10; factorial = factorialOfNumber(reminder); cout << "\nThe Factorial of "<< reminder << " = " << factorial; sum = sum + factorial; } cout << "\n\nThe Sum of the Factorials of " << number << " is = " << sum << "\n\n"; if(number == sum) { cout << number << " is a Strong Number"; } else { cout << number << " is Not a Strong Number"; } return 0; }
Please Enter the Number to Check for Strong Number = 40585
The Factorial of 5 = 120
The Factorial of 8 = 40320
The Factorial of 5 = 120
The Factorial of 0 = 1
The Factorial of 4 = 24
The Sum of the Factorials of 40585 is = 40585
40585 is a Strong Number
C++ Program to Print Strong Numbers between 0 and N
This C++ strong number example allows the user to enter the minimum and maximum values. Next, it prints the strong numbers between min and max value.
#include<iostream> using namespace std; long factorialOfNumber(int number) { if (number == 0 || number == 1) return 1; else return number * factorialOfNumber(number - 1); } long strongNumber(int number) { int temp, reminder; long sum = 0, factorial; temp = number; for(temp = number; temp > 0; temp = temp / 10 ) { factorial = 1; reminder = temp % 10; factorial = factorialOfNumber(reminder); sum = sum + factorial; } return sum; } int main() { int number, temp, minimum, maximum ; long sum = 0; cout << "\nPlease Enter Minimum and Maximum for Strong Numbers = "; cin >> minimum >> maximum; for(number = minimum; number <= maximum; number++ ) { temp = number; sum = strongNumber(temp); if(number == sum) { cout << number << " is a Strong Number" << endl; } } return 0; }
Please Enter Minimum and Maximum for Strong Numbers = 1 100000
1 is a Strong Number
2 is a Strong Number
145 is a Strong Number
40585 is a Strong Number