Program to check given number is strong number or not in C++. Strong Number Program in c++

 

Strong Number :-

    If the addition of factorial of each digit of a number is equal to original number then the number is a Strong Number.

Example :

Consider 145, the individual digits are 1, 4 & 5. 

1! + 4! + 5! = 1 + 24 + 120 = 145. As  the sum of factorial of each digit of 145 is 145, It is a Strong Number



Program to check given number is strong number or not

Code

#include<bits/stdc++.h>

using namespace std;


int main(){
    cout<<"Enter a Number"<<endl;         //Requesting a Number
int n;                                                //to store number
cin>>n;                                         //reading the number
int sum=0;                             //to store the sum of factorials
int p=0;                                //to store individual digit
int temp=n;                         //copy number to temp for future reference
while(n!=0){                            //loop to separate each digit
p=n%10;                         //last digit is stored in p
n=n/10;                         //deleting last digit of original number
int fact=1;                               //to store factorial
for(int i=1; i<=p; i++){                       //loop to calculate factorial
fact=fact*i;
}
sum=sum+fact;                         //adding factorial to sum
}
if(sum==temp){ //if sum matches original number it's a strong number
cout<<"It's a Strong Number"<<endl;
}
else{
cout<<"Not a Strong Number"<<endl;
}
return 0;
}


Output

Enter a Number
7
Not a Strong Number

Post a Comment

0 Comments