[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppRecursion
See also factorial
See also Fibonacci.
See also GCD.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppRecursion
(C++) Recursion
A function calling itself.Example 1: Factorial
int Factorial(const int n)
{
assert(n >= 0);
if (n==0) return 1;
return (n * Factorial(n-1));
}
See also factorial
Example 2: Fibonacci
int fibonacci(const int n)
{
if (1==n || 2==n)
{
return 1;
}
else
{
return fibonacci(n-2) + fibonacci(n-1);
}
}
See also Fibonacci.
Example 3: Greatest common divisor
int GreatestCommonDivisor(const int x, const int y)
{
if (y==0) return x;
return GreatestCommonDivisor(y,x%y);
}
See also GCD.
'Recursion' links
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
