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

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

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

To check for Strong Number, break the number into digits, find and add the factorial of each digit and lastly compare the result to the number. If both are equal then the number is a String Number else not a Strong Number.

For Example:145

Sum of factorial of digits of number   = 1! x 4! x 5145

Strong Number

Algorithm:-

  1. Take an input from user .
  2. Input is stored in an int type variable say ip.
  3. Input is stored in another int type variable, save.
  4. Initialize sum to 0
  5. A loop is started.
    1. In Every loop cycle factorial of last digit of the number will be found.
    2. Factorial will be added to an int type variable sum.
    3. ip will be divided by 10 i.e. value of ip will be changed.
    4. The loop will work until ip is greater than 0.
  6. sum will be compared with save
    1. If both are equal then the number is a Strong Number
    2. Otherwise the number is not a Strong Number.
strong Number or Not

C++ Code:-

//C++ program
    //Strong Number or not
    #include<iostream>
    using namespace std;
    //main Program
    int main()
    {
        int ip,sum=0;
        cout<<"Enter number to check: ";
            //user input
        cin>>ip;
        int save=ip;
        //logic to check for Strong Number starts
        while(ip)
        {
            int num=ip%10;
            int fact = 1;
            //finding factorial of each digit of input
            for(int i=num;i>0;i--)
            {
                fact=fact*i;
            }
            sum+=fact;
            ip/=10;
        }
        //checking for Strong Nunber
        if(sum==save)
        {
            cout<<save<<" is a Strong Number";
        }
        else
        {
            cout<<save<<" is not a Strong Number";
        }
        //logic ends
        return 0;
    }

Output

   
    Enter number to check: 145

    145 is a Strong Number

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


  • 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 “)