C++ program to find factorial of large numbers using array
In this program basically we multiply the number from 1 to the number and every time we store the value in array from left to right for e.g. ( if I want to store 129 into the array I will store it as 921 it will be easily to do calculations this way).
- Initially 1 will be stored in the ar[] and the ar_size will also be 1
- then it will enter int the of x from x=2 to x=number multiply x*ar[]
- and update the ar[]. To store the value in ar[] following operation will take place the loop runs from i=0 to ar_size and we will store the last digit int the variable p and update the ar[] and the rest of the digits will be stored in c and in the next loop we will have to store digit by digit in ar[].
Let’s take an example of 5
For x=2
ar[]=1, ar_size=1
p=2*ar[]=2*1 ,c=0
ar[]=2 ,c=0
For x=3
ar[]=2,ar_size=1
p=3*ar[]=3*2 ,c=0
ar[]=6 ,c=0
For x=4
ar[]=6,ar_size=1
p=4*ar[]=4*6=24 ,c=2
ar[]=4, c=2 ar[]=42,c=0
For x=5
ar[]=42,c=0
p=5*4=20
ar[]=0 ,c=2
p=5*2+c=12
ar[]=021
Consider the program:
Output
need an explanation for this answer? contact us directly to get an explanation for this answer