# factorial of large numbers

Page 1 of 1

## 3 Replies - 695 Views - Last Post: 30 July 2014 - 02:28 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=351259&amp;s=6c9f392bc0964fffc801620f95304bd0&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 blackalbus7

Reputation: 0
• Posts: 1
• Joined: 29-July 14

# factorial of large numbers

Posted 29 July 2014 - 02:20 PM

i have been doing coding challenges and while practicing i have encountered a problem and didn't know how to solve it.

by following some tutorials i have designed a code and still i dont get the desired output can anyone point out the bug
```#include <iostream>

using namespace std;

int fact(int,int);

int arr[200]={1};

int main()
{
int n,t,len=0,i=0,j,a;
cin>>t;

while(t--)
{
cin>>n;
a=n;
while(a>0)
{
arr[i]=a%10;
a=a/10;
len++;
i++;
}
cout<<len<<endl;
len=fact(n,len);
cout<<len<<endl;
for(j=len-1;j>0;j--)
{
cout<<arr[j]<<endl;
}
}
return 0;
}

int fact(int p,int m)
{
int temp=0,i=0,x;
while(i<=m)
{
if((i+1)>m && temp>0)
{
arr[i]=temp%10;
temp=temp/10;
m++;
i++;
if(temp==0)
{
break;
}
}
else
{
x=arr[i]*(p-1)+temp;
arr[i]=x%10;
temp=temp/10;
i++;
}
}
if(p>2)
fact(p-1,m);
else
return m;
}

```

Is This A Good Question/Topic? 0

## Replies To: factorial of large numbers

### #2 modi123_1

• Suitor #2

Reputation: 11617
• Posts: 45,791
• Joined: 12-June 08

## Re: factorial of large numbers

Posted 29 July 2014 - 02:23 PM

Quote

i dont get the desired output can anyone point out the bug

Explain! Explain any errors you are receiving.. Explain what you want your code to do, and explain what it is doing.

### #3 CTphpnwb

• D.I.C Lover

Reputation: 3466
• Posts: 12,350
• Joined: 08-August 08

## Re: factorial of large numbers

Posted 29 July 2014 - 05:29 PM

Factorials of large numbers can overflow int. Try unsigned long long.

### #4 David W

• DIC supporter

Reputation: 298
• Posts: 1,839
• Joined: 20-September 08

## Re: factorial of large numbers

Posted 30 July 2014 - 02:28 AM

What you really need ... is ... a BigInt class ...

and ...
if your check at my signature web links (below) you will find several simple examples there.