If u are required to calculate the factorial of large numbers like 100! Or 1000!, then it could become a headache for u as there are certain languages that provide support for large numbers like Java but there are some that do not like C and C++.

So, U can’t store such large values in their maximum range data type (long long in case of C). But there is one thing that would help u in this case.

The “ARRAY”. Array can have a large size so that we could store them.

One has a choice that whether he wants to use the character array or the integer array. Some prefer character array over integer but I don’t. According to me, character array make the program complex as it involves storing the numbers in reverse order, proper placing of the null character.

So, I decided to use the integer character but the main problem was how to limit the printing of the factorial as there is a null character in case of char array but nothing like that in int. To solve this problem, I came up with the solution by using the structure in the program. So, here is the program….

#include<stdio.h>

struct fact
{
	long long val[10000];
	int till;
	int zero;
};

typedef struct fact Fact;

Fact input[10000];

int main()
{
	long long temp=0,i,j,pro=0;
	int va;
	input[0].val[0]=1;
	input[0].till=1;
	for(i=1;i<1010;i++)
	{
		for(j=0;j<input[i-1].till;j++)
		{
			pro=(input[i-1].val[j]*(i+1))+temp;
			if(pro>=10)
			{
				input[i].val[j]=(pro%10);
				temp=pro/10;
				input[i].till=(j+1);
			}
			else
			{
				input[i].val[j]=pro;
				input[i].till=(j+1);
				temp=0;
			}
		}
		if(temp>0)
		{
			while(temp>0)
			{
				input[i].val[j]=temp%10;
				temp=temp/10;
				j++;
				input[i].till=j;
			}
		}
		temp=0;
	}
	scanf("%d",&va);
	for(j=input[va-1].till-1;j>=0;j--)
	{
		printf("%lld",input[va-1].val[j]);
	}
	return 0;
}
Advertisements