Factorial of Large Number in C and C++

Factorial of huge numbers contain such huge numbers of digits. For instance factorial of 100 has right around 158 digits.

So there is no information type accessible to store such a long worth. In any case,

we can discover factorial for huge numbers utilizing straightforward augmentation strategy that we utilized in our educational time. Beneath I have shared the program for it.

Program for Factorial of Large Number in C

#include<stdio.h>
     
int multiply(int x,int a[],int size)
{
	int carry=0,i,p;
    
	for(i=0;i<size;++i)
	{
		p=a[i]*x+carry;
		a[i]=p%10;
		carry=p/10;
	}
		
	while(carry!=0)
	{
		a[size]=carry%10;
		carry=carry/10;
		size++;		
	}    	
    	return size;
}
     
int main()
{  
	int n,a[1000],i,size=1;
    a[0]=1;
 
	printf("Enter any large number:");
    scanf("%d",&n);
    	
    for(i=2;i<=n;++i) {
		size=multiply(i,a,size);    		
    }
    	
    for(i=size-1;i>=0;--i)
    {
		printf("%d",a[i]);    	
    }
    	
   	return 0;
}

Program for Factorial of Large Number in C++


#include<iostream>
 
using namespace std;
 
int multiply(int x,int a[],int size)
{
	int carry=0,i,p;
    
	for(i=0;i<size;++i)
	{
		p=a[i]*x+carry;
		a[i]=p%10;
		carry=p/10;
	}
		
	while(carry!=0)
	{
		a[size]=carry%10;
		carry=carry/10;
		size++;		
	}    	
    	return size;
}
     
int main()
{  
	int n,a[1000],i,size=1;
    a[0]=1;
 
	cout<<"Enter any large number:";
    cin>>n;
    	
    for(i=2;i<=n;++i) {
		size=multiply(i,a,size);    		
    }
    	
    for(i=size-1;i>=0;--i)
    {
		cout<<a[i];    	
    }
    	
   	return 0;
}

Output

Factorial of Large Number in C and C++

Leave a Comment

error: Alert: Content is protected!!