Strong Numbers in a Given Range in C++
Strong Number in a Given Range
Today in this page we will be discussing the code to find the Strong Numbers in a Given Range in C++ programming language.
Strong number is a special number whose sum of the factorial of digits is equal to the original number.
For Example: 145 is strong number. Since, 1! + 4! + 5! = 145.
The idea is to iterate from (1, N) and check if any number between the range is a strong number or not. If yes then print the corresponding number, else we need to check for the next number.
Let’s understand this with help of some examples
Input: N = 100
Output: 1 2
Explanation:
Only 1 and 2 are the strong numbers from 1 to 100 because
1! = 1, and
2! = 2
Input: N = 1000
Output: 1 2 145
Explanation:
Only 1, 2 and 145 are the strong numbers from 1 to 1000 because
1! = 1,
2! = 2, and
(1! + 4! + 5!) = 145
Strong Numbers in a Given Range C++ Code
#include <iostream>
using namespace std;
int main()
{
int i, n, n1, s1 = 0, j, k, en, sn;
long fact;
sn = 1;
en = 200;
cout << "\n Find Strong Numbers within an range of numbers " << sn <<" and "<<en <<endl;
cout << " The Strong numbers are: ";
for (k = sn; k <= en; k++)
{
n1 = k;
s1 = 0;
for (j = k; j > 0; j = j / 10)
{
fact = 1;
for (i = 1; i <= j % 10; i++)
{
fact = fact * i;
}
s1 = s1 + fact;
}
if (s1 == n1)
cout << n1 << " ";
}
cout << endl;
}
Output
Find Strong Numbers within an range of numbers 1 and 200 The Strong numbers are: 1 2 145

Login/Signup to comment