Home » C++ programs » C++ Most popular & searched programs

C++ program to find factorial of large numbers using array

Learn: How to find factorial of large numbers in C++ using array, this program will explain finding the factorial of large number.
Submitted by Shubham Singh Rajawat, on June 05, 2017

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
p=3*ar[]=3*2 ,c=0
ar[]=6 ,c=0

For x=4
p=4*ar[]=4*6=24 ,c=2
ar[]=4, c=2        ar[]=42,c=0

For x=5
ar[]=0 ,c=2    

Consider the program:

using namespace std;

#define size 10000 

int fact(int x, int ar[], int ar_size);

void factorial(int n)
	int ar[size];
	ar[0] = 1;
	int ar_size = 1;
	for (int x=2; x<=n; x++)
		ar_size = fact(x, ar, ar_size);
	for (int i=ar_size-1; i>=0; i--)
		cout << ar[i];	


int fact(int x, int ar[], int ar_size)
	int c = 0; 
	for (int i=0; i< ar_size; i++)
	{   int p = ar[i] * x + c;
		ar[i] = p % 10; 
		c = p/10; 

	while (c)
		ar[ar_size] = c%10;
		c = c/10;
	return ar_size;
int main()

	int n;

	cout<<"Enter an integer number: ";

	cout<<"Factorial of "<<n<<" is:"<<endl;

	return 0;


First run:
Enter an integer number: 20 
Factorial of 20 is: 

Second run:
Enter an integer number: 250
Factorial of 250 is:

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.