Previous post: Next post:
Python #13-Let's Play with Python part-1-Fibonacci Series Python #15-How to get user input in python
Factorial of a given number in Python:
Example:
Explanation:
Line 3 : fact=1
Declaring variable fact and assigning value = 1.
Line 4 : number = int(input("Enter the number to find factorial:"))
This line is used to get the value from the keyboard from the user.
Line 6 : if number < 0:
This might be one of the logic ..as i used....
We cannot predict the factorial of given negative number. That's why i used this logic.
Line 8 : elif number is 0:
This is another predefined logic.(i.e) 0! is always 1.
Line 10 : else:
for x in xrange(1, number+1):
fact=fact*x
This is the main logic of our factorial program, such that running the for loop until the range is obtained (i.e) number+1. Multiplying the number with the before number until the loop ends.
Line 13: print fact
This is the print statement outside the loop statement. once the loops is over the fact value will be printed.
Python #13-Let's Play with Python part-1-Fibonacci Series Python #15-How to get user input in python
Factorial of a given number in Python:
What is Factorial of a number?
The product of an integer and all the integer below it.
Example:
Note: Use 🖉 to edit the code. Use ▶ to view the output.
Explanation:
Line 3 : fact=1
Declaring variable fact and assigning value = 1.
Line 4 : number = int(input("Enter the number to find factorial:"))
This line is used to get the value from the keyboard from the user.
Line 6 : if number < 0:
This might be one of the logic ..as i used....
We cannot predict the factorial of given negative number. That's why i used this logic.
Line 8 : elif number is 0:
This is another predefined logic.(i.e) 0! is always 1.
Line 10 : else:
for x in xrange(1, number+1):
fact=fact*x
This is the main logic of our factorial program, such that running the for loop until the range is obtained (i.e) number+1. Multiplying the number with the before number until the loop ends.
Line 13: print fact
This is the print statement outside the loop statement. once the loops is over the fact value will be printed.
Don't forget to drop your comments..!👀
Thanks and regards,
Tech bird
Comments