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
Methods:-
- Iterative
- 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
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 200+ courses offered by PrepInsta in One Subscription
- Positive or Negative number: C | C++ | Java | Python
- Even or Odd number: C | C++ | Java | Python
- Sum of First N Natural numbers: C | C++ | Java | Python
- Sum of N natural numbers: C | C++ | Java | Python
- Sum of numbers in a given range: C | C++ | Java | Python
- Greatest of two numbers: C | C++ | Java | Python
- Greatest of the Three numbers: C | C++ | Java | Python
- Leap year or not: C | C++ | Java | Python
- Prime number: C | C++ | Java | Python
- Prime number within a given range: C | C++ | Java | Python
- Sum of digits of a number: C | C++ | Java | Python
- Reverse of a number : C | C++ | Java | Python
- Palindrome number: C | C++ | Java | Python
- Armstrong number : C | C++ | Java | Python
- Armstrong number in a given range : C | C++ | Java | Python
- Fibonacci Series upto nth term : C | C++ | Java | Python
- Find the Nth Term of the Fibonacci Series : C | C++ | Java | Python
- Factorial of a number : C | C++ | Java | Python
- Power of a number : C | C++ | Java | Python
- Factor of a number : C | C++ | Java | Python
- Strong number : C | C++ | Java | Python
- Perfect number : C | C++ | Java | Python
- Automorphic number : C | C++ | Java | Python
- Harshad number : C | C++ | Java | Python
- Abundant number : C| C++ | Java | Python
- Friendly pair : C | C++ | Java | Python
Login/Signup to comment
#include
using namespace std;
int fact(int n)
{
int ans = 1;
for (int i = n; i > 0; i–)
{
ans = ans * i;
}
return ans;
}
int main()
{
int n;
int ans = 0;
cout << "enter the number to check strong number" <> n;
int i = n;
while (i != 0)
{
int rem = i % 10;
ans = ans + fact(rem);
cout << "fact<" << fact(rem) << endl;
i = i / 10;
}
if (ans == n)
{
cout << n << " is a strong number " << endl;
}
else
cout << n << " is not a strong number " << endl;
}
#include
using namespace std;
int fact(int n)
{
int m =1;
for(int i=n;i>0;i–)
{
m = m*i;
}
return m;
}
int main()
{
cout<<"Hello Dude!!! \n";
cout<>n;
int a=0;
int b =n;
while(n>0)
{
int rem = n%10;
a = a+fact(rem);
n =n/10;
}
// cout<<a;
if(a==b)
cout<<"The number is strong number";
else
cout<<"The number is not strong number";
return 0;
}
#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);
}
#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;
}
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 “)