Q:

Write C++ program to print all strong numbers between 2 numbers

0

Write C++ program to print all strong numbers between 2 numbers

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.

#include <iostream>
using namespace std;
 
// Function declarations
long long fact(int num);
void printStrongNumbers(int start, int end);
 
 
int main()
{
    int start, end;
 
    /* Input start and end range */
    cout<<"Enter the lower limit: ";
    cin>>start;
    cout<<"Enter the upper limit: ";
    cin>>end;
 
    cout<<"List of strong numbers between"<<start<<"to "<<end << " are: "<<endl;
    printStrongNumbers(start, end);
 
    return 0;
}
 
 
 
//Printing all strong numbers in a given range
void printStrongNumbers(int start, int end)
{
    long long sum;
    int num;
 
    // Iterates from start to end
    while(start != end)
    {
        sum = 0;
        num = start;
 
        // Calculating sum of factorial of digits
        while(num != 0)
        {
            sum += fact(num % 10);
            num /= 10;
        }
 
        // If sum of factorial of digits equal to current number
        if(start == sum)
        {
           cout<<start<<endl;
        }
 
        start++;
    }
}
 
//Recursively find factorial of any number
long long fact(int num)
{
    if(num == 0)
        return 1;
    else
        return (num * fact(num-1));
}

Result:

Enter the lower limit: 1

Enter the upper limit: 150

List of strong numbers between1to 150 are: 

1

2

145

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write C++ program to find prime numbers in given r... >>
<< Write C++ program to find Length of the String by ...