Factorial of a number (for loops)

In this case I have uploaded the code to Github here.

What this program does is ask the user for an integer, and then we do what we call sanitizing the entry, or handling exceptions.

The factorial of a number is the result of multiplying the integer numbers before it until we reach 1. This algorithm can’t be done to a negative number, and the factorial of 0 is 1. 

The way that we handled the exception is to be prepared to recieve an input that would make the program fail, so that when it happens our program knows what to do and not only crashes. In this case an if is implemented so that it verifies the given input. If the number is negative, then the program ignores it value and it takes the negative part from it and calculates the factorial of the given number.

After we have prepared for the human error, we now calculate the factorial of the number. For this we implemented a for loop. This loops are used when we know that we must apply a task a certain number of times, or when we want a variable that keeps track of the numberof times a section of code has executed. 

Basically the for loops needs a varaible that we use as counter, and we help ourselves with the range() function. This function returns the value of a series of succesive numbers between the given values, without including the upper limit.

range(0,10) gives us a list of numbers like this:

0 1 2 3 4 5 6 7 8 9

This is used to specify the values that our control variable will take inside the for loop.

This is for my of my

CC BY 4.0 Factorial of a number (for loops) by Manuel Lepe is licensed under a Creative Commons Attribution 4.0 International License.

Comments are closed.