C++ Program to check whether a number is a Strong Number or not

Strong Number in C++

Strong Numbers is a number in which the sum of the factorial of individual digits of the numbers is equal to the number itself.

For Example: 145 
145 = 1! x 4! x 5! =  145
Strong Number

Methods:-

  1. Iterative
  2. Recursive

Method 1

For input num

  • Initialize variable sum = 0
  • Extract digits of the num
  • For each extracted digit calculate factorial and add them to sum variable
  • If sum == num then we have found a strong number
Strong Number in C++

C++ Code:-

Run
#include <iostream>
using namespace std;

// function to calculate factorial
int facto(int n){
    int fact = 1;
    
    for(int i = 1; i <= n; i++)
            fact = fact * i;
    
    return fact;
}

int detectStrong(int num){
    
    int digit, sum = 0;
    int temp = num;
    
    // calculate 1! + 4! + 5!
    while(temp!=0){
        digit = temp % 10;
        
        sum = sum + facto(digit);
        temp /= 10;
    }
    
    // returns 1 if both equal else 0
    return sum == num;
    
}
int main ()
{
    int num = 145;
    
    if(detectStrong(num))
        cout << num << " is Strong Number";
    else
        cout << num << " is Not Strong Number";

}

Output

145 is Strong Number

Method 2 (Recursive)

Here we use recursion in c++ to find factorial of a number.

C++ Code:-

Run
#include <iostream>
using namespace std;

// function to calculate factorial
int facto(int num)
{
    if(num == 0)
        return 1;
        
    return num * facto(num-1);
}

int detectStrong(int num){
    
    int digit, sum = 0;
    int temp = num;
    
    // calculate 1! + 4! + 5!
    while(temp!=0){
        digit = temp % 10;
        
        sum = sum + facto(digit);
        temp /= 10;
    }
    
    // returns 1 if both equal else 0
    return sum == num;
    
}
int main ()
{
    int num = 145;
    
    if(detectStrong(num))
        cout << num << " is Strong Number";
    else
        cout << num << " is Not Strong Number";

}

Output

145 is Strong Number

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 150+ courses offered by PrepInsta in One Subscription

3 comments on “C++ Program to check whether a number is a Strong Number or not”


  • Satya Prakash

    #include
    using namespace std;
    void strongnumber(int n)
    {
    int sum =0;
    int fact =1;
    int temp =n;
    while(n)
    {
    int rem = n%10;
    fact =1;
    for(int i=1;i<=rem;i++)
    {
    fact= fact*i;
    }
    sum+=fact;
    n/=10;
    }
    if(temp == sum)
    cout<<temp<<"is a strong number";
    else
    cout<<temp<>n;
    strongnumber(n);
    }


  • Dheeraj

    #include

    using namespace std;

    int fact(int n)
    {
    if(n>n;
    string s=to_string(n);
    int sum=0;

    for(int i=0;i<=s.size()-1;i++)
    {
    sum=sum+fact(int(s[i]-48));

    }
    if(sum==n)
    {
    cout<<"strong";
    }
    return 0;
    }


  • Devansh

    num = int(input())
    temp = num
    lis = []
    lis2 = []
    pro = 1
    sum1 = 0
    while num>0:
    rem = num%10
    lis.append(rem)
    num = num//10
    for x in range(len(lis)):
    num1 = lis[x]
    while num1>0:
    pro = pro*num1
    num1 = num1-1
    lis2.append(pro)
    pro = pro//pro
    sum1 = sum(lis2)
    if(sum1 == temp):
    print(“is a Strong Number”)
    else:
    print(“is not a Strong Number “)