Testing the iterative factorial function - C++ Data Structure

C++ examples for Data Structure:Algorithm

Description

Testing the iterative factorial function

Demo Code

#include <iomanip>
#include <iostream>

unsigned long factorial(unsigned long);

int main(int argc, const char *argv[]) {
    for (int counter = 0; counter <= 10; ++counter) {
        std::cout << std::setw(2) << counter << "! = " << factorial(counter)
                  << std::endl;// www  .j av a2s  . c  o m
    }

    return 0;
}
// iterative function factorial
unsigned long factorial(unsigned long number) {
    unsigned long result = 1;

    // iterative factorial calculation
    for (unsigned long i = number; i >= 1; --i) {
        result *= i;
    }

    return result;
}

Result


Related Tutorials