C Program to Find Strong Number

In this post, we will learn how to check and find strong numbers using the C Programming language. But first let’s learn about strong numbers.

Strong numbers are the numbers whose sum of factorials of digits is equal to the original number. For example: 145 is a strong number. Since, 1! + 4! + 5! = 1 + 24 + 120 = 145.

We will calculate the sum of the factorial of the digits and then compare this sum with the original number. If the sum is equal to the original number, then the entered number is a strong number.

We will use the following methods to check strong numbers:

  1. Using While Loop
  2. Using For Loop 

So, without further ado, let’s begin this tutorial.

C Program to Find Strong Number

// C Program to Check Strong Number Using While Loop
#include <stdio.h>
int main(){
    int n, i, rem, temp, fact, sum = 0;
    
    // Asking for input
    printf("Enter a number to check strong number: ");
    scanf("%d", &n);
    
    temp = n;
    while (temp > 0){
        i = 1, fact = 1;
        rem = temp % 10;
        
        while (i <= rem){
            fact = fact * i;
            i++;
        }
        sum = sum + fact;
        temp = temp / 10;
    }
    
    // Displaying output
    if (n == sum){
        printf("%d is a strong number.", n);
    }
    else {
        printf("%d is not a strong number.", n);
    }
    return 0;
}

Output

Enter a number to check strong number: 145
145 is a strong number.

How Does This Program Work ?

    int n, i, rem, temp, fact, sum = 0;

In this program, we have declared six integer data type variables named n, i, rem, temp, fact and sum.

    // Asking for input
    printf("Enter a number to check strong number: ");
    scanf("%d", &n);

Now, the user is asked to enter a number to check for a strong number.

    temp = n;
    while (temp > 0){
        i = 1, fact = 1;
        rem = temp % 10;
        
        while (i <= rem){
            fact = fact * i;
            i++;
        }
        sum = sum + fact;
        temp = temp / 10;
    }

We have taken the help of a while loop to calculate the sum of the factorials of the digits.

Suppose, the user enters a number 145, then:

  • 1st While Loop Iteration: (145 > 0), here the condition is True. So, the loop continues. . . 
  • rem = temp % 10 = 145 % 10 = 5
    • 1st i While Loop Iteration: (1 <= 5), here the condition is True.
    • fact = fact * i = 1 * 1 = 1
    • 2nd i While Loop Iteration: (2 <= 5), here the condition is True.
    • fact = fact * i = 1 * 2 = 2
    • 3rd i While Loop Iteration: (3 <= 5), here the condition is True.
    • fact = fact * i =  2 * 3 = 6
    • 4th i While Loop Iteration: (4 <= 5), here the condition is also True.
    • fact = fact * i = 6 * 4 = 24
    • 5th i While Loop Iteration: (5 <= 5), here the condition is True.
    • fact = fact * i = 24 * 5 = 120
    • 6th i while loop Iteration: (6 <= 5), here the condition is False, so this loop terminates.

sum = sum + fact = 0 + 120 = 120
temp = temp / 10 = 145 / 10 = 14

  • 2nd While Loop Iteration: (14 > 0), here the condition is True. So, the loop continues. . . 
  • rem = temp % 10 = 14 % 10 = 4
    • 1st i While Loop Iteration: (1 <= 4), here the condition is True.
    • fact = fact * i = 1 * 1 = 1
    • 2nd i While Loop Iteration: (2 <= 4), here the condition is True.
    • fact = fact * i = 1 * 2 = 2
    • 3rd i While Loop Iteration: (3 <= 4), here the condition is True.
    • fact = fact * i =  2 * 3 = 6
    • 4th i While Loop Iteration: (4 <= 4), here the condition is also True.
    • fact = fact * i = 6 * 4 = 24
    • 5th i while loop Iteration: (5 <= 4), here the condition is False, so this loop terminates.

And we get: sum = sum + fact = 120 + 24 = 144
temp = temp / 10 = 14 / 10 = 1

  • 3rd While Loop Iteration: (1 > 0), here the condition is True. So, the loop continues. . . 
  • rem = temp % 10 = 1 % 10 = 1
    • 1st i While Loop Iteration: (1 <= 1), here the condition is True.
    • fact = fact * i = 1 * 1 = 1
    • 2nd i While Loop Iteration: (2 <= 1), here the condition is False, so the loop terminates and we get:

sum = sum + fact = 144 + 1 = 145
temp = temp / 10 = 1 / 10 = 0

  • 4th While Loop Iteration: (0 > 0), here the condition is False, so the while loop terminates and we get sum = 145.
    // Displaying output
    if (n == sum){
        printf("%d is a strong number.", n);
    }
    else {
        printf("%d is not a strong number.", n);
    }

Now, we check whether the sum is equal to the original number or not. If yes, then the entered number is a strong number otherwise the entered number is not a strong number.

C Program to Check Strong Number Using For Loop

// C Program to Check Strong Number Using For Loop
#include <stdio.h>
int main(){
    int number, i, rem, temp, fact, sum = 0;
    
    // Asking for input
    printf("Enter a number to check strong number: ");
    scanf("%d", &number);
    
    // Calculating the sum of factorials
    temp = number;
    for (temp = number; temp > 0; temp /= 10){
        fact = 1;
        rem = temp % 10;
        
        for (i = 1; i <= rem; i++){
            fact = fact * i;
        }
        sum = sum + fact;
    }
    
    // Displaying output
    if (number == sum){
        printf("%d is a strong number.", number);
    }
    else {
        printf("%d is not a strong number.", number);
    }
    return 0;
}

Output

Enter a number to check strong number: 40585
40585 is a strong number.

Conclusion

I hope after going through this post, you understand how to check and find strong numbers using the C Programming language.

If you have any doubt regarding the program, then contact us in the comment section. We will be delighted to assist you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *