0
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <iostream>
using namespace std; 
int Factorial(int iNum);

int main()
{
         int aNum;
         cout<<"Enter a number: ";
         cin>>aNum;
         cout<<Factorial(aNum)<<endl;
         system("PAUSE");
         return 0;
}
int Factorial(int iNum)
{
         cout<<"We are at number: "<<iNum<<endl;
         if (iNum <= 1)
         {  
                      cout<<"We are at number: ";
                      return 0;
         }
         else
         {
                      return Factorial(iNum - 1);
         }
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Using this code, I'm able to make it count from the number the user inputs down to zero. I need to find a way to count up from zero back up to the number, using recursion factorial again.
I've tried adding another function that's similar, but got no luck:

int Factorial2(int iNum)
{
         cout<<"We are at number: "<<iNum<<endl;
         if (1 >= iNum)
         {  
                      cout<<"We are at number: ";
                      return 0;
         }
         else
         {
                      return Factorial(iNum + 1);
         }
}

I've tried a lot of things.(Better than the above code too.)
Anyone have an idea how I can do this?

2
Contributors
2
Replies
5
Views
8 Years
Discussion Span
Last Post by FBIRyan
0

Well down is easy, you stop at a constant.

But counting up, the place where you stop is a variable.
Consider passing the current value AND the target value as parameters.

0

Well down is easy, you stop at a constant.

But counting up, the place where you stop is a variable.
Consider passing the current value AND the target value as parameters.

Thanks! Worked like a charm! :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.