Implementation of a factorial function
//---------------------------------------------------------
// Factorial.cpp
//
// Illustrate recursive factorial routine.
//
// D Searls
// Asbury College
// Apr 2002
//---------------------------------------------------------
#include <iostream>
#include <iomanip>
using namespace std;
//---------------------------------------------------------
// Function: factorial
//
// Precondition: 0 <= n <= 12
//
// Postcondition: the factorial of n is returned.
//
// In Parameter: n
//---------------------------------------------------------
int factorial(int n)
{
if (n == 0)
{
return 1;
}
else
{
return n*factorial(n-1);
}
}
//---------------------------------------------------------
// Function: factorial2
//
// Precondition: 0 <= n <= 12
//
// Postcondition: the factorial of n is returned.
//
// In Parameter: n
//---------------------------------------------------------
int factorial2(int n)
{
int f = 1;
for (int i = 1; i <= n; i++)
{
f = f * i;
}
return f;
}
//*********************************************************
// M A I N D R I V E R
//*********************************************************
int main()
{
for (int i = 0; i <= 14; i++)
{
cout << setw(2) << i << "! = " << setw(10) << factorial(i);
if (i > 12)
{
cout << " <== Wrong!";
}
cout << endl;
}
cout << endl;
for (int i = 0; i <= 14; i++)
{
cout << setw(2) << i << "! = " << setw(10) << factorial2(i);
if (i > 12)
{
cout << " <== Wrong!";
}
cout << endl;
}
cout << endl;
return 0;
}
Implementation of a power function.
//---------------------------------------------------------
// PowTest.cpp
//
// Implements and tests a recursive pow function.
//
// D Searls
// Asbury College
// October 2002
//---------------------------------------------------------
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
//---------------------------------------------------------
// power
//
// Returns: base raised to the exponent power.
//
// Precondition: exponent >= 0
//
// Illustrates a recursive function.
//
// In Parameters: base, exponent
//---------------------------------------------------------
double power(double base, int exponent)
{
if(exponent == 0)
{
return 1.0;
}
else
{
return base*power(base, exponent-1);
}
}
//---------------------------------------------------------
// power2
//
// Returns: base raised to the exponent power.
//
// Precondition: exponent >= 0
//
// In Parameters: base, exponent
//---------------------------------------------------------
double power2(double base, int exponent)
{
double p = 1.0;
for (int i = 0; i < exponent; i++)
{
p = p * base;
}
return p;
}
//*********************************************************
// M A I N D R I V E R
//*********************************************************
int main()
{
double theBase;
int theExponent;
cout << fixed << setprecision(0);
theBase = 2.0;
for(theExponent = 0; theExponent <= 20; theExponent++)
{
cout << theBase << '^' << theExponent << " = "
<< power(theBase, theExponent) << endl;
}
cout << endl;
for(theExponent = 0; theExponent <= 20; theExponent++)
{
cout << theBase << '^' << theExponent << " = "
<< power2(theBase, theExponent) << endl;
}
cout << endl;
for(theExponent = 0; theExponent <= 20; theExponent++)
{
cout << theBase << '^' << theExponent << " = "
<< pow(theBase, theExponent) << endl;
}
cout << endl;
return 0;
}