# Recursion in factorial (needs some extras)

Page 1 of 1

## 4 Replies - 1334 Views - Last Post: 30 April 2008 - 10:33 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=50635&amp;s=b82d14eab31874c5efc5a9f53a63e7f0&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ma7shykrombkteer

Reputation: 1
• Posts: 10
• Joined: 30-April 08

# Recursion in factorial (needs some extras)

Posted 30 April 2008 - 09:48 AM

Here is the question

It is interesting to watch recursion “in action.” Modify the factorial function of, to print
its local variable and recursive call parameter. For each recursive call, display the outputs
on a separate line and add a level of indentation. Do your utmost to make the outputs
clear, interesting and meaningful. Your goal here is to design and implement an output
format that helps a person understand recursion better. Print your output for each of the
following inputs, 5, 6, 7 and turn them in.

here is my code
```#include<iostream>
using namespace std;

int main()
{
int num, onum;
float fact = 1;

cin>>num;
onum = num;
cout<<endl;

while(num>=1)
{
fact = fact*num;
num = num-1;
cout<<"factorial: "<<fact<<endl;
}

cout<<endl<<"The final factorial of "<<onum<<" is: " <<fact<<endl;
system("pause");
return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: Recursion in factorial (needs some extras)

• g+ + -o drink whiskey.cpp

Reputation: 248
• Posts: 13,507
• Joined: 12-July 02

## Re: Recursion in factorial (needs some extras)

Posted 30 April 2008 - 09:57 AM

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

While I agree that code has been submitted, it is clear that you have not completed the assignment - what have you tried to do so?

Thanks.

### #3 ma7shykrombkteer

Reputation: 1
• Posts: 10
• Joined: 30-April 08

## Re: Recursion in factorial (needs some extras)

Posted 30 April 2008 - 10:08 AM

Amadeus, on 30 Apr, 2008 - 09:57 AM, said:

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

While I agree that code has been submitted, it is clear that you have not completed the assignment - what have you tried to do so?

Thanks.

I tried the following:
```#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
int num, onum, w=0;
float fact = 1;

cin>>num;
onum = num;
cout<<endl;

while(num>=1)
{
w=w+5;
fact = fact*num;
num = num-1;
cout<<setw(w)<<"factorial: "<<fact<<endl;
}

cout<<endl<<"The final factorial of "<<onum<<" is: " <<fact<<endl;
system("pause");
return 0;
}

```

what else should I do to make it more clear?

• g+ + -o drink whiskey.cpp

Reputation: 248
• Posts: 13,507
• Joined: 12-July 02

## Re: Recursion in factorial (needs some extras)

Posted 30 April 2008 - 10:31 AM

Perhaps you can add an output statement - something like "Factorial function is being called here", and an output statement that specifies the values of each variable being used (factorial, loop iteration, etc...) Also, do you need this as a separate function?

### #5 ma7shykrombkteer

Reputation: 1
• Posts: 10
• Joined: 30-April 08

## Re: Recursion in factorial (needs some extras)

Posted 30 April 2008 - 10:33 AM

I don't understand you.