--Originally published at Ervin Schlamme´s blog
In this #wsq06 we are going to learn how to create a factorial calculator. First of all we need to know what is a factoria operation (n!), which is the product of every number in a sequence in which n+1 is achieved. For example:
5!=1*2*3*4*5=120
The code for this operation is this one:
#include <iostream>
#include <string>
using namespace std;
int factorial (int n) // create a function of type integer that give the factorial value of n (n!)
{
if (n == 0)
{
return 1;
}
else
{
int recursion = factorial (n – 1);
int answer = n * recursion;
return answer;
}
}
int main ()
{
repeat:
string again;
int number;
int answer;
cout << “type number” << endl;
cin >> number;
answer = factorial (number);
cout << “The value for n! = ” << answer << endl; // prints the answer
ask:
cout << “Do yu want to try again?” << endl;
cin >> again;
if (again == “yes”) goto repeat;
if (again == “no”)
{
cout << “Come back soon!” << endl;
}
else
{
cout << “That is not a valid answer, try again simply typing yes or no.” << endl;
goto ask;
}
return 0;
}
in which the result must be like this:
I made a correction on “come back soon” on the code I just showed, in the image it says “Come back soos!”.
Oh, and by the way I used the next #masterytopics:
#masterytopics25
#masterytopics26
#masterytopics27
#masterytopics10
#masterytopics06
#masterytopics07