CPP Program to find all curious numbers between 100 to n

A curious number is a number which is the sum of factorial of all its digits.
Example:
145 = 1! + 4! + 5!

Program:

/*              cpp program to find curious numbers between 100 to n               */

#include"iostream"

using namespace std;

int main()
{
    int num, i, j, sum, fact, a, n, nf=0;
    do
    {
        cout<<"Enter a highest range[>100]: ";
        cin>>num;
        if(num < 100)
        {
            cout<<"Number must be greater than 100.\n";
        }
    }while(num < 100);
   
    for(i=100; i<=num; i++)
    {
        n=i;
        sum = 0;   
       
        while(n!=0)
        {
            a=n%10;
            n=n/10;
            fact = 1;
            for(j=a; j>=1; j--)
            {
                fact = fact * j;
               
            }
            sum = sum + fact;
           
        }
        if(sum == i)
        {
            cout<<sum<<endl;
            nf++;
        }
       
    }

    if(nf==0)
    {
        cout<<"No curious number found"<<endl;
    }
}


Output:

 

Comments

Popular Posts