/* factorial3.c
This shows how factorial can be computed, both non-recursively as
well as recursively.
Recursion is used here to compute the answer using return values.
Sample run:
[bert] ~/demos/functions> factorial3
Please enter a value for factorial computation: 4
Non-recursive answer is: 24
Please enter a value for recursive factorial computation: 5
Factorial of 5 is: 120
[bert] ~/demos/functions>
*/
#include <iostream>
using namespace std;
long factorial( long n)
{
if( n == 0)
return 1;
else
return ( n * factorial( n-1));// recursive call
}
int main()
{
int i, number;
int answer = 1;
//Non-recursively find factorial of small non-zero number
cout < "Please enter a value for factorial computation: ";
cin > number;
for (i=number; i>1; i--)
answer = answer * i;
cout < "Non-recursive answer is: " < answer < "\n";
// Now do it recursively
cout < "Please enter a value for recursive factorial computation: ";
cin > number;
cout < "Factorial of "<number<" is: "< factorial( number) < "\n";
return 0;
}
/* reverseDigits.cpp
This shows how input digits can be displayed in reverse order,
both non-recursively as well as recursively.
Recursion is used here to display the answer a piece at a time
on the way *into* the recursion. This is called "head" recursion.
Sample run:
[bert] ~/demos/functions> reverseDigits
Non-recursively print a number's digits in reverse order.
E.g. for 2468: 8642
Recursively print a number's digits in reverse order.
E.g. for 1357: 7531
*/
#include <iostream>
using namespace std;
void printReverse( int number)
{
while (number > 0) {
cout< number % 10;
number /= 10;
}
cout< endl;
}
void recursivePrintReverse(int number)
{
cout < number % 10;
number= number / 10;
if (number)
recursivePrintReverse( number);
else
cout < '\n';
}
int main()
{
cout< "Non-recursively print a number\'s digits in reverse order. \n";
cout< " E.g. for 2468: ";
printReverse( 2468);
cout< "Recursively print a number\'s digits in reverse order. \n";
cout< " E.g. for 1357: ";
recursivePrintReverse( 1357);
return 0;
}
/* reverseText.cpp
Recursively reverse input text. It is not possible
to do this non-recursively for variable sized input
without storing it temporarily in some data structure
such as an array.
Recursion is used here to display the answer a piece at
a time on the way back *out* of the recursion, which is
called "tail" recursion.
Sample run:
[bert] ~/demos/functions> reverseText
Enter the text to be reversed, followed by a dollar ($) sign:This is
Cool stuff! $
!ffutslooCsisihT
[bert] ~/demos/functions>
*/
#include <iostream>
using namespace std;
void reverse()
{
char c;
cin > c;// Note this skips white space
if (c != '$') {
reverse();
cout < c;
}
}
int main()
{
cout< "Enter the text to be reversed, followed by a dollar ($) sign:";
reverse();
cout< endl;
return 0;
}
/* recursionExamples.cpp
Illustrate various recursive functions. Functions F1, F2,
and F3 taken from Adams, et. al. "C++: An Introduction to
Computing," Prentice Hall, 1995.
Results of run:
[bert] ~/demos/functions> recursionExamples
Call sample function using F1( 1, 5)
6
Call sample function using F1( 8, 3)
0
Call sample function using F2( 2, 4)
8
This implemented multiplication
Call sample function using F3( 256)
2
This gives first digit of input
Call sample function using F4( 2, 5)
32
This gives exponentiation.
*/
#include <iostream>
using namespace std;
int F1(int num1, int num2)
{
if (num1 > num2)
return 0;
else if (num2 == (num1 + 1) )
return 1;
else
return F1( num1 + 1, num2 - 1) + 2;
}
double F2( double x, unsigned n)
{
if (n == 0)
return 0;
else
return x + F2( x, n-1);
}
unsigned F3( int n)
{
if (n < 0)
return F3(-n);
else if (n < 10)
return n;
else
return F3( n / 10);
}
double F4( double x, int n)
{
if (n == 0)
return 1.0;// anchor case
else if (n >0)
return F4( x, n - 1) * x;// inductive step
else {
cerr < "\n Negative exponent *** exiting *** \n";
return -1.0;
}
}
int main()
{
cout< "Call sample function using F1( 1, 5)\n";
cout< F1( 1, 5);
cout< "\n Call sample function using F1( 8, 3)\n";
cout< F1( 8, 3);
cout< "\n Call sample function using F2( 2, 4)\n";
cout< F2( 2, 4);
cout< "\n This implemented multiplication\n";
cout< "\n Call sample function using F3( 256)\n";
cout< F3( 256);
cout< "\n This gives first digit of input\n";
cout< "\n Call sample function using F4( 2, 5)\n";
cout< F4( 2, 5);
cout< "\n This gives exponentiation.\n" < endl;
return 0;
}