1. Programming questions:
Write a program that calculates the factorial of a number and calculates the sum of the digits of that factor.
Define the sum of digits in factorial() function with num as the parameter.
Within the function, the factorial of num is first computed, and then the sum of the numbers in the factorial is returned.
The factorial of the number num is the product of all numbers from 1 to num. For example, if factorial 5 is 1 * 2 * 3 * 4 * 5 = 120, then the sum of its factorial numbers is 1 + 2 + 0 = 3.
Example input: 24
Example output: 81
2. Implementation:
Editable ** as follows:
#!/usr/bin/python3.9
# -*coding: utf-8 -*
## copyright (c) 2024 , inc. all rights reserved
## @time : 2024/1/29 20:40
# @author : fangel
# @filename : 85.Factorial digits. py
# @software : pycharm
def sum_of_digits_in_factorial(num):
multiply = 1
Step 1: Calculate the factorial first.
for i in range(1,num+1):
multiply = multiply * i
sum = 0
Step 2: Convert the calculation result into a string and add them one by one.
for strtmp in str(multiply):
sum += int(strtmp)
return sum
Get user input.
num = int(input())
Call the function.
print(sum_of_digits_in_factorial(num))
3. Analysis: Dynamic incentive plan in February This question first calculates the factorial of a certain number, which can be judged in a loop, and then converts the result into a string and then finds the sum of each number.
4. Running result:
Input: 10 Output: 27