//////////////////////////////////////////////////////// // // triangle.cpp // // Started by Jeff Ondich 10/6/99 // Last modified 10/6/99 // // The function Triangle() computes the Nth // triangular number (1 + 2 + 3 +...+ N) // recursively. I have left output statements // in the code to trace the function call/return // sequence. // //////////////////////////////////////////////////////// #include int Triangle( int N ); int main( void ) { int N; cout << "Number please: "; cin >> N; cout << "The " << N << "th triangular number is " << Triangle( N ) << endl; return( 0 ); } ////////////////////////////////////////////////// // // If N >= 1, Triangle returns the Nth // triangular number. Otherwise, it returns 1. // ////////////////////////////////////////////////// int Triangle( int N ) { // The output statements in this function are // not indented so they will be noticeably // not part of the real work of the function. cout << "Starting Triangle(" << N << ")" << endl; int returnValue; if( N <= 1 ) returnValue = 1; else returnValue = Triangle( N-1 ) + N; cout << "Finishing Triangle(" << N << ")" << endl; return( returnValue ); }