~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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?

Recommended Answers

All 2 Replies

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.

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! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.