# Thread: simplify recursive function

1. ## simplify recursive function

I am attempting the challenge at the end of the recursion tutorial on this site to return the factorial of any number.

I have come up with 2 ways to do it, but both seem a bit cumbersome.

Code:
```#include <iostream>

using namespace std;

void Recurse(int number,int factorial)
{
if (number == 1)
{
cout << "The factorial of your number is: " << factorial << ".\n";
return;
}

Recurse(number-1,factorial*(number-1));
}

int main()
{
int number;
cout << "Please enter a positive number:";
cin >> number;

if (number < 0)
cout << "You entered a Negative Number!\nExiting...\n";
else
if (number == 0)
cout << "The Factorial of your number is 1.\n";
else
Recurse(number,number);

return 0;
}```
Code:
```#include <iostream>

using namespace std;

// number is the number entered by the user.
// count is going to increment by 1, starting at 1.
// factorial is going to hold the product of all numbers from 1 to number.
void Recurse(int number, int count, int factorial)
{
if (number < 0) // return if the input was negative.
{
cout << "You Entered a Negative Number.\n";
return;
}
if ((count-1) == number) // This check includes 0 as to return a factorial of 1.
{
cout << "The factorial of your number is: " << factorial << ".\n";
return;
}

Recurse(number,count+1,factorial*count);
}

int main()
{
int number;
cout << "Please enter a positive number:";
cin >> number;

Recurse(number,1,1);

return 0;
}```
I feel that there is too much code for something so simple, how could I rewrite these to simplify things a little?

Thanks.

2. Originally Posted by MattJ812
I am attempting the challenge at the end of the recursion tutorial on this site to return the factorial of any number.

I have come up with 2 ways to do it, but both seem a bit cumbersome.

Code:
```#include <iostream>

using namespace std;

void Recurse(int number,int factorial)
{
if (number == 1)
{
cout << "The factorial of your number is: " << factorial << ".\n";
return;
}

Recurse(number-1,factorial*(number-1));
}

int main()
{
int number;
cout << "Please enter a positive number:";
cin >> number;

if (number < 0)
cout << "You entered a Negative Number!\nExiting...\n";
else
if (number == 0)
cout << "The Factorial of your number is 1.\n";
else
Recurse(number,number);

return 0;
}```
Code:
```#include <iostream>

using namespace std;

// number is the number entered by the user.
// count is going to increment by 1, starting at 1.
// factorial is going to hold the product of all numbers from 1 to number.
void Recurse(int number, int count, int factorial)
{
if (number < 0) // return if the input was negative.
{
cout << "You Entered a Negative Number.\n";
return;
}
if ((count-1) == number) // This check includes 0 as to return a factorial of 1.
{
cout << "The factorial of your number is: " << factorial << ".\n";
return;
}

Recurse(number,count+1,factorial*count);
}

int main()
{
int number;
cout << "Please enter a positive number:";
cin >> number;

Recurse(number,1,1);

return 0;
}```
I feel that there is too much code for something so simple, how could I rewrite these to simplify things a little?

Thanks.
you can let your recursion function return an int. something like this would do.
Code:
```int recurse( int number)
{
if( number == 1)
return 1;
else
return number*recurse(number-1);
}```

Popular pages Recent additions