C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Loop Programs in C

Check whether a number is Strong Number (loop)

Introduction

A Strong number (also called a factorial number) is a number that equals the sum of the factorials of its digits.

Examples:

  • 145 = 1! + 4! + 5! = 1 + 24 + 120 = 145 →  Strong number
  • 40585 = 4! + 0! + 5! + 8! + 5! = 40585 →  Strong number
  • 123 = 1! + 2! + 3! = 9 →  Not a Strong number

 

C Program: Check whether a number is Strong Number (loop)

Method 1: Using for loop

C

#include <stdio.h>

 

int main() {

    int num, original, remainder, sum = 0;

 

    // Input

    printf("Enter a positive integer: ");

    scanf("%d", &num);

 

    // Validate input

    if (num < 0) {

        printf("Invalid input! Please enter a positive number.\n");

        return 1;

    }

 

    original = num;

 

    // Calculate sum of factorials of digits using for loop

    for (int temp = num; temp != 0; temp /= 10) {

        remainder = temp % 10;

        int fact = 1;

 

        // calculate factorial of the digit

        for (int i = 1; i <= remainder; i++) {

            fact *= i;

        }

 

        sum += fact; // add factorial to sum

    }

 

    // Compare sum with original number

    if (sum == original)

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

    else

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

 

    return 0;

}

Output

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

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

OUTPUT 3 :
Enter a positive integer: 40585
40585 is a Strong number.

 

Explanation

  1. Take a positive integer as input.
  2. Store the value in original for comparison.
  3. Use a for loop to extract each digit of the number:
    • Find the last digit with % 10.
    • Calculate its factorial using another for
    • Add the factorial to the running total sum.
  4. Compare sum and original.
  5. If they are equal → Strong number, else not.

 

C Program: Check whether a number is Strong Number (loop)

Method 2: Using while loop

C

#include <stdio.h>

 

int main() {

    int num, original, remainder, sum = 0, i, fact;

 

    // Input

    printf("Enter a positive integer: ");

    scanf("%d", &num);

 

    // Validate input

    if (num < 0) {

        printf("Invalid input! Please enter a positive number.\n");

        return 1;

    }

 

    original = num;

 

    // Check Strong number

    while (num != 0) {

        remainder = num % 10;  // extract last digit

        fact = 1;

 

        // calculate factorial of each digit

        for (i = 1; i <= remainder; i++) {

            fact *= i;

        }

 

        sum += fact;   // add factorial to sum

        num /= 10;     // remove last digit

    }

 

    // Compare sum with original number

    if (sum == original)

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

    else

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

 

    return 0;

}

Output

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

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

OUTPUT 3 :
Enter a positive integer: -145
Invalid input! Please enter a positive number.


Explanation

  1. The user enters a positive integer.
  2. The number is stored in original for later comparison.
  3. Each digit is extracted using % 10.
  4. The factorial of each digit is calculated using a for
  5. The factorial values are summed.
  6. If the sum equals the original number → it’s a Strong number.

 

C Program: Check whether a number is Strong Number (loop)

Method 3: Using do..while loop

C

#include <stdio.h>

 

int main() {

    int num, original, remainder, sum = 0, fact, i;

 

    // Input

    printf("Enter a positive integer: ");

    scanf("%d", &num);

 

    // Validate input

    if (num < 0) {

        printf("Invalid input! Please enter a positive number.\n");

        return 1;

    }

 

    original = num;

 

    // Check Strong number using do...while loop

    do {

        remainder = num % 10;   // extract last digit

        fact = 1;

 

        // calculate factorial of the digit

        for (i = 1; i <= remainder; i++) {

            fact *= i;

        }

 

        sum += fact;   // add factorial to sum

        num /= 10;     // remove last digit

 

    } while (num != 0);

 

    // Compare sum with original number

    if (sum == original)

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

    else

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

 

    return 0;

}

Output

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

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

OUTPUT 3 :
Enter a positive integer: 40585
40585 is a Strong number.

Explanation

  1. Take a positive integer as input.

  2. Store it in original for comparison later.

  3. Use a do...while loop to process digits:

    • Extract the last digit using % 10.

    • Compute its factorial using a nested for loop.

    • Add the factorial to sum.

    • Remove the last digit using /= 10.

  4. Continue until the number becomes 0.

  5. Compare the sum with the original number.