Calculating Factorial (Recursively & Iteratively)

pradeep's Avatar author of Calculating Factorial (Recursively & Iteratively)
This is an article on Calculating Factorial (Recursively & Iteratively) in C.
Calculating the factorial of a number is a basic excercise while learning to program in C, many of us have done it iteratively, it can also be done recursively. I am posting both iterative and recursive versions below.

Code: C
/* Recursive Version */
 unsigned int recursive_factorial(int n)
  {
      return n>=1 ? n * recr_factorial(n-1) : 1;
  }
   
 /* Iterative Version */
  unsigned int iter_factorial(int n)
  {
      int f = 1;
      int i;
      for(i = 1; i <= n; i++)
     {
          f *= i;
      }
      return f;
  }
bothie's Avatar
Go4Expert Member
loved your functions they are just readable
deepak.mobisy's Avatar, Join Date: Oct 2007
Newbie Member
Thanks for this kind of efoorts.
oleber's Avatar, Join Date: Apr 2007
Go4Expert Member
Good work but one small comment :

Why having int and unsigned int?

Just for coherence, and avoiding compilers work:

Code:
/* Recursive Version */
unsigned int recursive_factorial(unsigned int n) 
{
    return n >= 1 ? n * recr_factorial(n-1) : 1;
}
Code:
/* Iterative Version */
unsigned int iter_factorial(unsigned int n) 
{
    unsigned int f = 1;
    for(unsigned int i = 1; i <= n; i++) 
    {
        f *= i;
    }
    return f;
}

shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Offtopic comment:
Oleber, I would suggest using the code block rather than manually coloring the code blocks. - http://www.go4expert.com/misc.php?do=bbcode#code
pr1nc3k1d's Avatar, Join Date: Dec 2007
Go4Expert Member
Everything looks good and nice but what's if you want to calculate " 1000! " or the factorial of greater values ? The " unsigned int " type can memorize a value between 0 and +4,294,967,295 but " 1000! " is more much greater than the dimension of " long double " which is the greatest data type in C/C++. I think this is a good question. I'm waiting suggestions and ideas.
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
The greatest has also the limitation for large numbers and I think you are with the greatest "long double"
pr1nc3k1d's Avatar, Join Date: Dec 2007
Go4Expert Member
Yes, it has, but i'm thinking on an algorithm which not calculates the result,but it generates it into a vector or a list. For example you can get a number with more than 1000 digits as result But if I put every digit of the number into a list I could view it and print it on the screen or into a file. So I think it could be a possible solution for this problem because there is no other data types which could memorize such a number. Opinions ?
pr1nc3k1d's Avatar, Join Date: Dec 2007
Go4Expert Member
I made it but I don't really like it cuz` it's slow. It took about 6-7 minutes waiting for the result of " 1000! ".

Code:
#include <iostream.h>
#include <conio.h>
#include <time.h>
void main ()
{
	long int v[259000],i,n;
   double start;
   clrscr();
   v[0]=1;
   for(i=1;i<259000;i++) v[i]=0;
   cout<<"Enter the number: "; cin>>n;
   if(n==0 || n==1 ) cout<<n<<"!="<<"1";
   else
   {
      start = clock ();
   	long int c=0;
   	for(i=1;i<=n;i++)
      {
      	long int j;
     		for(j=0;j<=c;j++)
           v[j]*=i;
         for(j=0;j<=c;j++)
         {
         	if(v[j]>=10)
            {
            	v[j+1]=v[j+1]+v[j]/10;
               v[j]=v[j]%10;
               int k1=258999,cont=0;
               while(v[k1]==0) { cont++; k1--; }
               c=258999-cont;
            }
         }
        cout<<i<<"!=";
        for(j=c;j>=0;j--)
        		cout<<v[j];
        cout<<endl;
   	}
   start=clock()-start;   
   cout<<"It took "<<start<<" seconds to complete. ";
  }
   getch();
}
pradeep's Avatar, Join Date: Apr 2005
Team Leader
Goodness gracious! 6-7 mins?? Try to rethink your logic!