Q. Write an algorithm and program to check Strong Number?

Program to check Strong Number : This section focuses on Strong Number algorithm and program. This programs should be practiced to improve the coding skills required for various interviews (campus interviews, walk-in interviews), coding rounds etc.

Strong Number : Strong number is a number whose sum of all digits factorial is equal to the original number.
Given a number n and our task is to check the sum of digit factorials is equal to original number or not.

For Example :-

Input : n = 145
Output : Yes, The number is strong number
Explanation : As we can see that input number is 145. Now, we need to see using loop that the sum of factorial of each digit is equal to original number or not. So sum of each digit factorial will be 1 + 24 + 120 = 145. So, output for the following code is "Yes, The number is strong number". Now let us understand what logic we need to use in this :
1. Firstly, we need to use loop and extract each digit using modulus.
2. For each digit calculate the factorial. Now calculate the sum of all digit factorials.

Algorithm To Check Strong Number

\\Algorithm To Check Strong Number
START
Step 1 : Initialize sum of factorials as 0. 
Step 2 : For every digit d, do following
            Add d! to sum of factorials.
Step 3 : If sum factorials is same as given.
            number, return true.
Step 4 : Else return false
Stop


Program To Check Strong Number

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • //C Program To Check Strong Number
    #include <stdio.h> 
      
    int main()
    {
        int fact = 145; 
        int f[10];
        
        // Fills factorials of digits from 0 to 9.
        f[0] = f[1] = 1; 
        for (int i = 2; i<10; ++i) 
            f[i] = f[i-1] * i;
            
        int sum = 0; 
        // Traverse through all digits of fact. 
        int temp = fact; 
        while (temp) 
        { 
            sum += f[temp%10]; 
            temp /= 10; 
        } 
        
        //check if sum equal to orignal number
        if(sum == fact)
        {
            printf("Yes, The number is strong number");
        }
        else
        {
            printf("No, The number is not strong number");
        }
    
        return 0;
    }
    
    //C++ Program To Check Strong Number
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int fact = 145; 
        int f[10];
        
        // Fills factorials of digits from 0 to 9.
        f[0] = f[1] = 1; 
        for (int i = 2; i<10; ++i) 
            f[i] = f[i-1] * i;
            
        int sum = 0; 
        // Traverse through all digits of fact. 
        int temp = fact; 
        while (temp) 
        { 
            sum += f[temp%10]; 
            temp /= 10; 
        } 
        
        //check if sum equal to orignal number
        if(sum == fact)
        {
            cout<<"Yes, The number is strong number";
        }
        else
        {
            cout<<"No, The number is not strong number";
        }
        return 0;
    }
    
    //Java Program To Check Strong Number
    
    public class LFC
    {
        public static void main(String[] args) {
        
            int fact = 145; 
            int f[] = new int[10];
        
            // Fills factorials of digits from 0 to 9.
            f[0] = f[1] = 1; 
                for (int i = 2; i<10; ++i) 
                    f[i] = f[i-1] * i;
                
            int sum = 0; 
            // Traverse through all digits of fact. 
            int temp = fact; 
            while (temp>0) 
            { 
                sum += f[temp%10]; 
                temp /= 10; 
            } 
            
            //check if sum equal to orignal number
            if(sum == fact)
            {
                System.out.println("Yes, The number is strong number");
            }
            else
            {
                System.out.println("No, The number is not strong number");
            }
            
        }
    }
    
    
    #Python Program To Check Strong Number
    
    sum=0  
    num=145   
    temp=num  
    while(num):  
        i=1    
        fact=1  
        rem=num%10  
        while(i<=rem):  
            fact=fact*i   # Find factorial of each number  
            i=i+1  
        sum=sum+fact  
        num=num//10  
    if(sum==temp):  
        print("Yes, The number is strong number")  
    else:  
        print("No, The number is not strong number")
    
    //C# Program To Check Strong Number
    
    using System;
    class LFC {
      static void Main() {
            int fact = 145; 
            int []f = new int[10]; 
        
            // Fills factorials of digits from 0 to 9.
            f[0] = f[1] = 1; 
                for (int i = 2; i<10; ++i) 
                    f[i] = f[i-1] * i;
                
            int sum = 0; 
            // Traverse through all digits of fact. 
            int temp = fact; 
            while (temp>0) 
            { 
                sum += f[temp%10]; 
                temp /= 10; 
            } 
            
            //check if sum equal to orignal number
            if(sum == fact)
            {
                Console.WriteLine("Yes, The number is strong number");
            }
            else
            {
                Console.WriteLine("No, The number is not strong number");
            }
      }
    }
    
    <?php
    //PHP Program To Check Strong Number
    
    $x = 145;  
    $f[10] = array(); 
    
    // Fills factorials of digits from 0 to 9.
    $f[0] = $f[1] = 1;  
    for ($i = 2; $i < 10; ++$i)  
        $f[$i] = $f[$i - 1] * $i;
    
    $factSum = 0;  
    // Traverse through all digits of fact.
    $temp = $x;  
    while ($temp>1)  
    {  
        $factSum += $f[$temp % 10];  
        $temp = (int)$temp / 10; 
    } 
           
    //check if factSum equal to orignal number
    if($factSum == $x)
        echo "Yes, The number is strong number\n"; 
    else
        echo "No, The number is not strong number\n";  
    
    //C Program To Check Strong Number
    #include <stdio.h> 
      
    int main()
    {
        int fact = 145; 
        int f[10];
        
        // Fills factorials of digits from 0 to 9.
        f[0] = f[1] = 1; 
        for (int i = 2; i<10; ++i) 
            f[i] = f[i-1] * i;
            
        int sum = 0; 
        // Traverse through all digits of fact. 
        int temp = fact; 
        while (temp) 
        { 
            sum += f[temp%10]; 
            temp /= 10; 
        } 
        
        //check if sum equal to orignal number
        if(sum == fact)
        {
            printf("Yes, The number is strong number");
        }
        else
        {
            printf("No, The number is not strong number");
        }
    
        return 0;
    }
    

    Output

    Yes, The number is strong number
    

    Recommended Programs

       Program to find perfect number
       Program to count number of digits in a number