Hello everyone, today we are going to write a program in python to find the factorial of a given number. So get ready with your code editor as we are going to do this program in the best way possible.
If you want the full program, you can go to the end of this article otherwise it would be good if you stick with us as we are going to go through every step that you should follow to improve your problem solving skills.
The factorial of a number is the product of the number and all the terms preceding it till 1. For example, if you are asked to find the factorial of 5, you would do: 5 x 4 x 3 x 2 x 1 = 120
At this stage you must have understood that to make this program run, you should have a prior knowledge of loops. So basically, the steps would be to firstly ask the user for a number, and then to loop and multiply the number with the numbers preceding them.
*As I have told you the steps, you can try to write the program on your.*
Here I will tell you step wise what you need to do.
Here, we need the user to give us the number we need to find the factorial of. We need to convert this to float so that we can perform calculations on it.
The code of this step is as follows:
Now this process is somewhat complex. We need to take two variable, namely b and product. Here, b is taken as a variable that has to be iterated in the while loop. Initially, b is taken equal to num1. The product variable is firstly initialized equal to b.
In the while loop, the condition is set that b>=2. We have taken product to be equal to product*(b-1). Because that's what we want. We want the initial number to be multiplied by its predecessor and to continue so on. And then we have iterated the value of b by -1 so it decreases by 1 each time the loop repeats.
Also, as we have to finally present these result to the user, we need to convert these float values to string so that they can be printed. Hence we used the str command to convert them to strings.
The code snippet for this step is as follows:
Hence, we are able to find the factorial of the given number.
The final code looks something like this:
So this was the final code. I hope you were able to learn something new. Please subscribe to our newsletter for further updates on programming tutorials. If you have any doubts feel free to ask in the comment section below. We will be happy to answer that.
If you want the full program, you can go to the end of this article otherwise it would be good if you stick with us as we are going to go through every step that you should follow to improve your problem solving skills.
Overview:
The factorial of a number is the product of the number and all the terms preceding it till 1. For example, if you are asked to find the factorial of 5, you would do: 5 x 4 x 3 x 2 x 1 = 120
At this stage you must have understood that to make this program run, you should have a prior knowledge of loops. So basically, the steps would be to firstly ask the user for a number, and then to loop and multiply the number with the numbers preceding them.
*As I have told you the steps, you can try to write the program on your.*
Understanding what we need to do:
Here I will tell you step wise what you need to do.
Step 1: Taking the input from user:
Here, we need the user to give us the number we need to find the factorial of. We need to convert this to float so that we can perform calculations on it.
The code of this step is as follows:
Step 2: Finding the factorial using loops:
Now this process is somewhat complex. We need to take two variable, namely b and product. Here, b is taken as a variable that has to be iterated in the while loop. Initially, b is taken equal to num1. The product variable is firstly initialized equal to b.
In the while loop, the condition is set that b>=2. We have taken product to be equal to product*(b-1). Because that's what we want. We want the initial number to be multiplied by its predecessor and to continue so on. And then we have iterated the value of b by -1 so it decreases by 1 each time the loop repeats.
Also, as we have to finally present these result to the user, we need to convert these float values to string so that they can be printed. Hence we used the str command to convert them to strings.
The code snippet for this step is as follows:
Hence, we are able to find the factorial of the given number.
The final code looks something like this:
So this was the final code. I hope you were able to learn something new. Please subscribe to our newsletter for further updates on programming tutorials. If you have any doubts feel free to ask in the comment section below. We will be happy to answer that.
Again, thanks for reading!!!
Thank you for this program!
ReplyDelete