C++ Problem Assignment: "Iterative Factorial"
Write an iterative function that will calculate the factorial of a positive whole number.
Purpose:
To apply Iterative Factorial to illustrate the concept of Recursion.
Lab Help:
Here is a sample output:
Enter an integer value and I will display its factorial:
7
The factorial of 7 is 5040
Below is the lab solution that should give you a clear direction on the steps that you need to do to solve the programming problem. Please go over the solution provided to help you develop your own solution. Please don't just copy the solution and upload it back to me, try to come up with your own approach.
Lablo_NCC_Recursion_Iterative_Factorial.cpp
This file contains the main function. Program execution begins and ends there.
Chapter Programming Challenge 1: Iterative Factorial
#include <iostream>
using namespace std;
Function prototype:
int factorial(int);
int main()
{
int number;
cout << "Enter an integer value and I will display its factorial: ";
cin >> number;
cout << "The factorial of " << number << " is " << factorial(number) << endl;
return 0;
}
Definition of factorial function:
This function uses iteration to calculate the factorial of the argument. The function returns the factorial.
int factorial(int num)
{
int fac = 1;
for (int i = 1; i <= num; i++)
{
fac *= i;
}
return fac;
}