# Calculating Factorial (Recursively & Iteratively)

Discussion in 'C' started by pradeep, Oct 30, 2006.

1. ### pr1nc3k1dNew Member

Joined:
Dec 6, 2007
Messages:
11
0
Trophy Points:
0
Occupation:
Student
Location:
Romania
Did you try the code ? It's working fine ( no overflow ) but it's also limited. I didn't say that it's not. I wanted to write a code which calculates the factorial of 1000 but it's also working for greater values. The vector can hold a result with more than 10.000 digits , but it's limited.

Joined:
Jan 9, 2008
Messages:
356
14
Trophy Points:
0
Occupation:
Developer
Location:
NOIDA

First i told you to store in vector too much overhead and second thing storing has some limit!!! But you know today In Statistical Software factorial of very big number is required. So on that case, By any way you have to go for sterling Approximation formula.

Joined:
Jan 9, 2008
Messages:
356
14
Trophy Points:
0
Occupation:
Developer
Location:
NOIDA
I got the solution for any Big number!!! This code is very efficient and optimized implemented by limked list..
Code:
```class Link
{
//Data Member Parts
private:
int dta;

// Function Member Parts

public:
{
;
}
{
return nxt;
}

{
return prv;
}
void ExtraInsert(Link* p);          // After Inserting
int getDta(void)
{
return dta;
}
void SetDta(int temp)
{
dta = temp;
}
int GetNoOfElem();
void DeleteMe(void);
void ClearAll(void);
};

{
int count = 0;

while(p->NxtLnk()!=NULL){
count++;
p = p->NxtLnk();
}

count++;
return count;
}

{
if(prv == NULL) { hd = this;}
p->nxt = nxt;
p->prv = this;
nxt = p;
if(p->nxt == NULL){rear = p;}
};

{
if(hd == this){ //this is the first node
return this;
}

while(p->prv != NULL){
p = p->prv;
}

return p;
}

{
if(rear == this){
return this;
}

while(p->nxt != NULL){
p = p->nxt;
}

return p;
}

//***** Start ************//
{
if(prv == NULL) // First Element
{
nxt->prv = NULL;
hd = nxt;  // Next will be first one
delete this;
return;
}
if(nxt == NULL)
{
prv->nxt = NULL; // last node
rear = prv; //Previous node will be first
return;
}
prv->nxt = nxt;
delete this;
}
//****  End Of Function*******//

{
while(p1->NxtLnk() != NULL){
p2 = p1;
p1 = p1->NxtLnk();
delete p2;
}
delete p1;
};

int main()
{
int n;// remainder
int cy  // Carried over
int rst;
int N;

cout<<"Plz input the number:";
cin>>N;
for(int n=1;n<=N;n++)
{
rn = carry = 0;

while(p->NxtLnk() != NULL)
{
rst = p->getDta()*n+cy
if(rst>=10){
rn = rst%10;
carry = rst/10;
p->SetDta(rn);
}
else
{
p->SetDta(rst);
}
p = p->NxtLnk();
carry = rst/10;
}
rst = p->getDta()*n+cy

//Other allocated memory for carried over
while(rst >= 10){
p->SetDta(rst%10);//rnder
rst = rst/10;
p = p->NxtLnk();
}
p->SetDta(rst);
}

p = p->ToRear();
while(p->PrvLnk()!=NULL)
{
cout<<p->GetDta();
p=p->PrvLnk();
}
cout<<p->GetDta()<<endl;
int num = p->GetNoOfElem();
if(num >=5)
{
p = p->ToRear();
cout<<endl<<"Or"<<endl<<endl;

cout<<p->GetDta()<<".";
p = p->PrvLnk();

for(int i=1;i<5;i++)
{
cout<<p->GetDta();
p = p->PrvLnk();
}
cout<<"E"<num-1<endl;
}
p->ClearAll();
return 0;
}```

Last edited by a moderator: Jan 15, 2008

Joined:
Jul 12, 2004
Messages:
15,328
377
Trophy Points:
83
asadullah.ansari, Learn to use Code block when you have code snippets in posts

5. ### pr1nc3k1dNew Member

Joined:
Dec 6, 2007
Messages:
11
0
Trophy Points:
0
Occupation:
Student
Location:
Romania

Is this code working cuz I tried it and it's not !?

Joined:
Jan 9, 2008
Messages:
356
14
Trophy Points:
0
Occupation:
Developer
Location:
NOIDA
Yes!!! It's working. which plateform you r trying...

7. ### vignesh_svNew Member

Joined:
Jan 21, 2008
Messages:
9
0
Trophy Points:
0
very good job buddy could u help me by saying which is the header file to b included for using
the command sleep( ); in c

8. ### debleena_doll2002New Member

Joined:
Feb 5, 2008
Messages:
119
0
Trophy Points:
0
Why tou are asking this question in this thread. Just create new thread for it in Queries

Joined:
Feb 14, 2008
Messages:
77
0
Trophy Points:
0
good info

10. ### crazytolearn57New Member

Joined:
Feb 14, 2008
Messages:
48
0
Trophy Points:
0
good one

Joined:
Feb 13, 2008
Messages:
82