Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Create factorialNumber.Py
Q1) Count the number of trailing 0's in a factorial number.
- Loading branch information
Showing
1 changed file
with
24 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
def get_factorial(n): | ||
if n < 0: | ||
return -1 #if input is 0 break function | ||
else: | ||
factorial = 1 | ||
for i in range(1, n+1): # finds all number to multiply | ||
factorial *= i # number multiplied by the indices | ||
return factorial | ||
|
||
def trailingZeros(n): | ||
count = 0 | ||
i = 5 | ||
while(n//i != 0): # function continues until n is no longer divisibly by 5 | ||
count += int(n/i) # counts how many 5 there are | ||
i = i * 5 # can be used for any number to figure out the zeros | ||
return count | ||
|
||
|
||
n = int(input("Type in a number: ")) # User input | ||
fact = get_factorial(n) | ||
|
||
print("The factorail number of your input is " + str(fact) + ".") | ||
print("And the number of trailing zeros is: " + str(trailingZeros(n))) |