Question: For this snippet in C + + you will implement factorial both iteratively and recursively. For the iterative implementation simply take in the value to

For this snippet in C++ you will implement factorial both iteratively and recursively.
For the iterative implementation simply take in the value to factorialize and call it x. Create a for loop counting backwards from x to 0(meaning stop after the 1 and before the 0). Create a variable fact =1 before the loop. Call the counting variable of the loop i. for (i=x, i>0, i--); //the loop control. Inside the loop you execute fact = fact *I; //this is the creation of the factorial. Print the result.
For the recursive version use:
long fact (int x1){
long ans=1;
if (x1==1) return (1); // trivial case
else {
ans = x1* fact(x11); // the reducing step
return(ans);
}
}
[HINT: factorials get very big, very fast, be careful of overflow]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!