Tail Recursion |  ; A tail recursive function to calculate factorial ; A wrapper over factTR ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; unsigned factTR ( unsigned int n , unsigned int a ) { if ( n == 1 ) return a ; return factTR ( n - 1 , n * a ) ; } unsigned int fact ( unsigned int n ) { return factTR ( n , 1 ) ; } int main ( ) { cout << fact ( 5 ) ; return 0 ; }