lundi 28 décembre 2020

f(123) = 1! + 2! + 3! .I wrote a code for this type of function to work, have a look at it. My code is not giving desired result

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int a=123,c,h=0,fact=1,sum=0;

    for(int i=1;a>10;i++)
    {
        a/=pow(10,i);
        h++;
    }
    for(h;h>=0;h--)
    {
        c=a/pow(10,h);
        for(int j=1;j<=c;j++)
        {
            fact*=j;
        }
        sum+=fact;
        a-=(c*pow(10,h));
    }
    cout<<sum;
}

While using debugger I noticed that the line

c=a/pow(10,h);

is not working.

Here I have assigned a=123, but I want take it as input from the user. The variable a can be of any number of digits.

Can anyone suggest some other logic other than the one used in this code.

Aucun commentaire:

Enregistrer un commentaire