Strong number in C
×


Strong number in C

39

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 = 40585

 Since 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>

// 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;
}

Output:

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.




Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments