Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C++ Exercises: Find Strong Numbers within a range of numbers

C++ Numbers: Exercise-46 with Solution

Write a program in C++ to find Strong Numbers within a range of numbers.

Sample Solution:

C++ Code:

#include <iostream>
using namespace std;

int main()
{
    int i, n, n1, s1 = 0, j, k, en, sn;
    long fact;
    cout << "\n\n Find Strong Numbers within an range of numbers:\n";
    cout << "----------------------------------------------------\n";
    cout << " Input starting range of number: ";
    cin >> sn;
    cout << " Input ending range of number: ";
    cin >> en;
    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;
}

Sample Output:

Check whether a number is Strong Number or not:                                                
 -------------------------------------------------------                                             
 Input starting range of number: 1  
 Input ending range of number: 500  
 The Strong numbers are: 1  2  145

Flowchart:

Flowchart: Find Strong Numbers within a range of numbers

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C++ to check whether a number is a Strong Number or not.
Next: C++ Sorting and Searching Exercises Home

What is the difficulty level of this exercise?