Strong Number
PROBLEM :
Write a program to check whether a number is strong or not. A number is called strong number if sum of the factorial of its digit is equal to number itself. For example: 145 as 1! + 4! + 5! = 1 + 24 + 120 = 145
Input:
First line contains number of test cases T. Then following T lines contains an integer N.
Output:
Output "Strong" if given number is strong else "Not Strong" .
Constraints:
1<=T<=50
0<=N<=1000
Example:
Input:
2
145
10
Output:
Strong
Not Strong
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int Is_Strong(int ) ;
int factorial(int ) ;
int main()
{
int t,no ;
cin>>t ;
while(t--)
{
cin>>no ;
if(Is_Strong(no))
cout<<"Strong" ;
else
cout<<"Not Strong" ;
cout<<endl ;
}
return 0;
}
int Is_Strong(int no)
{
int r,sum,k ;
sum=0 ;
k=no ;
while(no)
{
r=no%10 ;
sum=sum+factorial(r) ;
no=no/10 ;
}
if(k==sum)
return 1 ;
return 0 ;
}
int factorial(int no)
{
int i,sum ;
sum=1 ;
if(no==0||no==1)
return 1 ;
for(i=1;i<=no;i++)
sum=sum*i ;
return sum ;
}
---------------------------------------------------------------------------------
Write a program to check whether a number is strong or not. A number is called strong number if sum of the factorial of its digit is equal to number itself. For example: 145 as 1! + 4! + 5! = 1 + 24 + 120 = 145
Input:
First line contains number of test cases T. Then following T lines contains an integer N.
Output:
Output "Strong" if given number is strong else "Not Strong" .
Constraints:
1<=T<=50
0<=N<=1000
Example:
Input:
2
145
10
Output:
Strong
Not Strong
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int Is_Strong(int ) ;
int factorial(int ) ;
int main()
{
int t,no ;
cin>>t ;
while(t--)
{
cin>>no ;
if(Is_Strong(no))
cout<<"Strong" ;
else
cout<<"Not Strong" ;
cout<<endl ;
}
return 0;
}
int Is_Strong(int no)
{
int r,sum,k ;
sum=0 ;
k=no ;
while(no)
{
r=no%10 ;
sum=sum+factorial(r) ;
no=no/10 ;
}
if(k==sum)
return 1 ;
return 0 ;
}
int factorial(int no)
{
int i,sum ;
sum=1 ;
if(no==0||no==1)
return 1 ;
for(i=1;i<=no;i++)
sum=sum*i ;
return sum ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment