Thursday, 14 November 2013

C PROGRAM TO CALCULATE FACTORIAL OF 100


I have made a program which can calculate factorial upto 16000 in Turbo C and 100000 in Visual Studio. But before going to that programs I would like to tell you that using array how we can perform multiplication of two numbers .If we learn to multiply two numbers using array than we can calculate factorial of any number .Therefore to explain it I have made a simple program which can calculate factorial from 1 to 100




 SOURCE CODE OUTPUT EXPLANATION DOWNLOAD


#include<conio.h>
#include<stdio.h>
void main()
{
  char choice;
  int e,i,j,f=0,ch[2][170],a,c,b=0,d;
  clrscr();
  for(j=0;j<=169;j++)
  {
    ch[0][j]=0;
    ch[1][j]=0;
  }
  do
  {
    clrscr();
    printf("\n\n\n\nEnter the no.between 0-101 whose factorial to be found ");
    scanf("%d",&a);
    if(a<1||a>100)
    {
      printf("\nFactorial cannot be calculated");
      getch();
      exit(0);
    }
    c=a-1,d=c;
    for(j=160;j>0;j--)
    {
ch[0][j]=a%10;
ch[1][j]=a%10;
a=a/10;
    }
    while(d!=0)
    {
      e=c/10,b=c%10;
      for(j=0;j<=160;j++)
      {
 ch[0][j]=ch[0][j]*b;
 ch[1][j]=ch[1][j+1]*e;
      }
      for(i=0;i<2;i++)
      {
for(j=160;j>0;j--)
{
 f=ch[i][j]/10;
 ch[i][j]=ch[i][j]%10;
 ch[i][j-1]+=f;
}
      }
      for(j=160;j>0;j--)
      ch[0][j]+=ch[1][j];
      for(j=160;j>0;j--)
      {
f=ch[0][j]/10;
ch[0][j]=ch[0][j]%10;
ch[0][j-1]+=f;
      }
      for(j=0;j<=160;j++)
      ch[1][j]=ch[0][j];
      d=d-1,c=d;
    }
    d=0;
    for(j=0;j<=160;j++)
    {
      if(ch[0][j]!=0)
      break;
      else
      d++;
    }
    for(j=d;j<=160;j++)
    printf("%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
I will tell you step by step how to calculate factorial of 100.You can see that there are eight for loops in my program .We will take any two numbers and see how this two numbers are multiplied through this for loops .Let the user type 99 than a=99, c=98 and d=98.We will follow the same steps which we do in actual multiplication.

                                                                  99
                                                           
                                                               * 98
                                                              ----------
                                                                 792
                                                         
                                                               8910
                                                            -------------
                                                               9702
Step 1:

First we will break 99 and put each number in single array variable as follows.


 ch[0][154]     ch[0][155]    ch[0][156]    ch[0][157]     ch[0][158]    ch[0][159]    ch[0][160]


      0


0

0

0

0

9

9


ch[1][154]      ch[1][155]    ch[1][156]    ch[1][157]     ch[1][158]    ch[1][159]    ch[1][160]

  
     0


0

0

0

0

9

9

         This all can be done by following fragment of code.         

for(j=160;j>0;j--)

    {

            ch[0][j]=a%10;
  
          ch[1][j]=a%10;
    
          a=a/10;
    }       


Step 2 :

Now we will perform multiplication. Just remember what we do in actual 

multiplication.

e=c/10=9,  b=c%10=8.


ch[0][154]     ch[0][155]    ch[0][156]    ch[0][157]     ch[0][158]    ch[0][159]    ch[0][160]

      
       0


0

0

0

0

72

72


ch[1][154]     ch[1][155]    ch[1][156]    ch[1][157]     ch[1][158]    ch[1][159]    ch[1][160]

     
      0


0

0

0

81

81

0

This all we will do by following fragment of code
   
    e=c/10,b=c%10;
  
    for(j=0;j<=160;j++)
  
    {
   
           ch[0][j]=ch[0][j]*b;
   
           ch[1][j]=ch[1][j+1]*e;
    
     }


Step 3:


 We have to add carry to one higher significant position to the left i.e. 


ch[0][154]     ch[0][155]    ch[0][156]    ch[0][157]     ch[0][158]    ch[0][159]    ch[0][160]

       
       0


0

0

0

7

9

2


ch[1][154]     ch[1][155]    ch[1][156]    ch[1][157]     ch[1][158]    ch[1][159]    ch[1][160]

      
       0


0

0

8

9

1

0

This all we will do by following fragment of code

for(i=0;i<2;i++)
  
{
         
   for(j=160;j>0;j--)
          
  {
           
     f=ch[i][j]/10;
             
     ch[i][j]=ch[i][j]%10;
             
     ch[i][j-1]+=f;
           
  }
    
}


Step 4:

Now we will add this numbers in following way


ch[0][154]     ch[0][155]    ch[0][156]    ch[0][157]     ch[0][158]    ch[0][159]    ch[0][160]

     
       0


0

0

0

7

9

2


ch[1][154]     ch[1][155]    ch[1][156]    ch[1][157]     ch[1][158]    ch[1][159]    ch[1][160]

    
       0


0

0

8

9

1

0


ch[0][154]     ch[0][155]    ch[0][156]    ch[0][157]     ch[0][158]    ch[0][159]    ch[0][160]


      0


0

0

8

16

10

2

This all we will do by following fragment of code

for(j=160;j>0;j--)
  
   ch[0][j]+=ch[1][j];


Step 5:

 We have to add carry to one higher significant position to the left i.e. 


 ch[0][154]     ch[0][155]    ch[0][156]    ch[0][157]     ch[0][158]    ch[0][159]    ch[0][160]

     
      0


0

0

9

7

0

2

This all we will do by following fragment of code
    
  for(j=160;j>0;j--)
    
  {
       
      f=ch[0][j]/10;
      
      ch[0][j]=ch[0][j]%10;
           
      ch[0][j-1]+=f;
   
    }

Step 6:

Now we will copy above result in ch[1][j].


ch[0][154]     ch[0][155]    ch[0][156]    ch[0][157]     ch[0][158]    ch[0][159]    ch[0][160]

     
       0


0

0

9

7

0

2


ch[1][154]     ch[1][155]    ch[1][156]    ch[1][157]     ch[1][158]    ch[1][159]    ch[1][160]

  
       0


0

0

9

7

0

2

This all we will do by following fragment of code
                            
     for(j=0;j<=160;j++)
                           
       ch[1][j]=ch[0][j];

Now d=98-1=97  ,c=d=97 and the whole process is repeated until d becomes 0 

and the factorial of 99 is stored in array.Now we will print the numbers.It should 

be noted that before printing the numbers ,I have used following loops .It is added 

because if some one type 5 than we will see only 120 gets print and rest of the 0 

coming before 120 will not get print.

    d=0;
  
  for(j=0;j<=160;j++)

   {
   
   if(ch[0][j]!=0)

      break;

     else

      d++;


   }
Any questions regarding to program please write in comments.

RELATED POST:-

No comments:

Post a Comment