C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Functions in C

Function to check strong number

C Program: Function to check strong number

C

#include <stdio.h>

 

// Function declarations

int factorial(int n);

int isStrong(int num);

 

int main() {

    int number;

 

    // Input number

    printf("Enter a number: ");

    scanf("%d", &number);

 

    // Function call

    if (isStrong(number))

        printf("%d is a Strong number.\n", number);

    else

        printf("%d is not a Strong number.\n", number);

 

    return 0;

}

 

// Function to calculate factorial of a digit

int factorial(int n) {

    int fact = 1;

    for (int i = 1; i <= n; i++)

        fact *= i;

    return fact;

}

 

// Function to check if number is Strong

int isStrong(int num) {

    int original = num;

    int sum = 0, digit;

 

    while (num > 0) {

        digit = num % 10;

        sum += factorial(digit);

        num /= 10;

    }

 

    if (sum == original)

        return 1;  // Strong number

    else

        return 0;  // Not strong

}

Output

 
OUTPUT 1 :
Enter a number: 145
145 is a Strong number.

OUTPUT 2 :
Enter a number: 123
123 is not a Strong number.

Explanation

  1. What is a Strong Number?
    A number is called a Strong number if the sum of the factorials of its digits equals the number itself.
    Example:
    • 145 = 1! + 4! + 5!
    • 145 = 1 + 24 + 120 = 145
  2. Functions Used:
    • factorial(int n) — Computes factorial of a digit.
    • isStrong(int num) — Sums factorials of digits and compares with original number.