Friday, 15 November 2013

C PROGRAM TO CALCULATE FACTORIAL OF 1000


I have posted a program which can calculate factorials upto 1000.As I have already explained how to calculate factorial of 100.Now its time to go 1 step further .If you have not read my previous post in which I have told how to calculate factorial of 100 than please first read it.




 SOURCE CODE OUTPUT EXPLANATION DOWNLOAD


#include<conio.h>
#include<stdio.h>
void main()
{
  FILE *fp;
  char choice;
  int g,e,i,j,f=0,ch[3][2571],a,c,b=0,d;
  clrscr();
  for(i=0;i<=2;i++)
  {
    for(j=0;j<=2570;j++)
    ch[i][j]=0;
  }
  printf("\n\n\n\n\n\t\t\t     MOHAMMED UJJAIN WALA\n\n\t\t\t\t  PRESENTS");
  printf("\n\n\n\t\t\t\t  FACTORIAL");
  printf("\n\n\n\npress any key to continue");
  getch();
  do
  {
    clrscr();
    fp=fopen("fact.txt","w");
    printf("Enter the no.between 0-1001 whose factorial to be found ");
    scanf("%d",&a);
    if(a<1||a>1000)
    {
      printf("\nFactorial cannot be calculated");
      getch();
      exit(0);
    }
    c=a-1,d=c,g=a;
    for(i=0;i<3;i++)
    {
      for(j=2568;j>0;j--)
      {
ch[i][j]=a%10;
a=a/10;
      }
      a=g;
    }
    while(d!=0)
    {
      b=0;
      for(i=0;i<3;i++)
      {
for(j=2568;j>0;j--)
{
 e=c%10;
 ch[i][j]=ch[i][j]*e;
}
c=c/10;
      }
      for(i=0;i<3;i++)
      {
for(j=2568;j>0;j--)
{
 f=ch[i][j]/10;
 ch[i][j]=ch[i][j]%10;
 ch[i][j-1]+=f;
}
      }
      for(i=0;i<3;i++)
      {
for(j=0;j<=2568;j++)
ch[i][j]=ch[i][j+b];
b=b+1;
      }
      for(j=2568;j>0;j--)
      ch[0][j]=ch[1][j]+ch[0][j]+ch[2][j];
      for(i=0;i<3;i++)
      {
for(j=2568;j>0;j--)
{
 f=ch[i][j]/10;
 ch[i][j]=ch[i][j]%10;
 ch[i][j-1]+=f;
}
      }
      for(j=0;j<2568;j++)
      {
ch[2][j]=ch[0][j];
ch[1][j]=ch[0][j];
      }
      d=d-1,c=d;
    }
    d=0;
    for(j=0;j<=2568;j++)
    {
      if(ch[0][j]!=0)
      break;
      else
      d++;
    }
    for(j=d;j<=2568;j++)
    {
      printf("%d",ch[0][j]);
      fprintf(fp,"%d",ch[0][j]);
    }
    printf("\nWant to enter more number whose factorial to be calculated(Y/N)");
    choice=getch();
  }while(choice=='y'||choice=='Y');
}

SCREENSHOTS

Previous                                                                 Next
We have seen in my previous post that if we how we can multiply two large numbers using array and can calculate factorial up to 100. This program follows the same steps as followed in program to calculate factorials upto 100, only some minute changes are required .Here we have declared array ch[3][2571] .Actually I have already calculated that how many columns are required in single row in which I  can store factorial of 1000.Here I have declared 3 rows because c and d can have maximum three digit number.
I have also included some input output functions through which we can store our factorial of 1000 in secondary memory. Remember that you have to extract all the files first than file fact.txt will get created in which your factorial gets stored.
We will create a file with name fact.txt in which we will store our factorial.

Any questions regarding to program please write in comments.


RELATED POST:-

No comments:

Post a Comment