For WSQ#9 we had to write a code who calculates the Factorial number of “n”.

For the ones who doesn’t know what a factorial number is (including me, 20 minutes ago) a factorial number means a function which multiplies a serial of descending natural numbers.

For example:

4! = 4x3x2x1 = 24

So the rule is:

n! = n × (n−1)!

And to do that in a C++ program, we have to develop a bit more complex process than that, so it would look like this:

/* Source code to find factorial of a number. */
#include <iostream>
using namespace std;
int main()
{
  int x , factorial=1;

    cout << "Please enter a non-negative number:  " << endl;
    cin >> x;

    for(int a=1; a<=x; a++)
    {
        factorial=factorial*a;
    }

cout << "The factorial of your number is " << factorial << endl;
cout << "Would you like to play again?   ";
string answer;
cin >> answer;

do
{
    int x , factorial=1;

    cout << "Please enter a non-negative number:  " << endl;
    cin >> x;

    for(int a=1;a<=x;a++)
    {
        factorial=factorial*a;
    }

    cout << "The factorial of your number is " << factorial << endl;
    cout << "Would you like to play again  ? ";
    cin >> answer;

}while (answer =="yes");

if (answer== "no")
{
  cout << "AS YOU WISH MASTER, MAY THE FORCE BE WITH YOU! " << endl;
}

return 0;
}

And also, as you can see in the code, at the end the program will ask the User if he would like to play again or quit, and if he quits a message will show up on his screen.

So the program will basically look like this!

FactorialCalculation Execution

As always I will leave my codes on the GitHub account and if you have any doubts feel completely free to ask me on the comments section!

Have a nice day, and remember to keep practicing

-The Admin.

CC BY 4.0 A Factorial Calculator! by esaupreciado is licensed under a Creative Commons Attribution 4.0 International License.