Strong number in C
0 942
checking if 40585 is a Strong Number
For the number 40585, let's calculate the sum of the factorial of its digits:
4! = 4*3*2*1 = 24 0! = 1 5! = 5*4*3*2*1 = 120 8! = 8*7*6*5*4*3*2*1 = 40320 5! = 5*4*3*2*1 = 120 Sum of factorial of digits = 24 + 1 + 120 + 40320 + 120 = 40585Since the sum of the factorial of its digits (40585) is equal to the original number (40585), 40585 is a strong number. Example:
// Program to check number is strong or not #include<stdio.h>Output:// Function to calculate factorial of a number int factorial(int n) { return (n == 0 || n == 1) ? 1 : n * factorial(n - 1); } int isStrongNumber(int num) { int originalNum = num, sum = 0; while (num > 0) { sum += factorial(num % 10); num /= 10; } return (sum == originalNum); } int main() { int num = 40585; // Example number // Check if the number is a strong number if (isStrongNumber(num)) { printf("%d is a Strong Number.\n", num); } else { printf("%d is not a Strong Number.\n", num); } return 0; }
40585 is a Strong Number.Description of the Program: Include the standard input-output header file stdio.h. Define a function factorial() to calculate the factorial of a number. Define a function isStrongNumber() to check if a number is a strong number or not. In the main() function: Ask the user to enter a number. Call the isStrongNumber() function to check if the entered number is a strong number. Print the result accordingly. Compile and run the program.
Share:




Comments
Waiting for your comments