--Originally published at The Clueless Programmer
So what this wsq asked us, was to do a program that got the factorial of a number, which to people that don´t know (like me before I looked it up on Wikipedia) is the product of all the numbers that precede a number. Example, the factorial of 4 is 24 (4*3*2*1). And then it asked us to continually asked the user if he wanted to get the factorial of another number until he said no. When I first got into this assignment I didn´t know what to do or where to start because I hadn´t understood the whole concept yet, but I got some help from this webpage and from the blogpost of one of my classmates, and then I knew what to do.
So here´s the program:
So as you can see first I name the variables and explain the program and then I start the recursion function, I started it after I introduced the program because I don´t want that to keep popping of over and over. Then I set my control variable to 1, it is important to do it here so that the value resets everytime the program starts again.
After this I create an “if” in which, if the value is negative or zero, the program says that has no factorial and sets the repetitive contition to “y”, so the program starts over. Then I start another loop that multiplies a variable equaled to the number with the variable equaled to one, later substracting one from the first variable while this is bigger than than one.
The program is basically over at this point, the only thing left would be to print the value to the user and then ask him if he wants to do it again. The answer to this question
Continue reading "WSQ – 06, Factorial calculator"