Demonstrate recursion : function recursion « Function « C++ Tutorial






#include <iostream> 
using namespace std; 
 
int f(int n); 
 
int main() 
{ 
  // use recursive version 
  cout << "4 factorial is " << f(4); 
 
  return 0; 
} 
 
int f(int n)  
{ 
  int answer; 
 
  if(n==1)  
      return(1); 
  
  answer = f(n-1)*n; 
  return(answer); 
}
4 factorial is 24








7.11.function recursion
7.11.1.Demonstrate recursion
7.11.2.Print a string backwards using recursion
7.11.3.Recursive factorial function
7.11.4.The recursive fibonacci function.
7.11.5.The iterative factorial method.