Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,657 questions

55,409 answers

573 users

How to sum the digit of a factorial of a number in Python

2 Answers

0 votes
def sum_digits(num):
    sum = 0
    
    while num != 0:
        sum += num % 10
        num //= 10
        
    return sum

def factorial(n):
    if n == 1 or n == 0:
        return 1
    return n * factorial(n - 1)


number = 9

result = factorial(number)

print("factorial =", result)
print("sum digits =", sum_digits(result))




'''
run:

factorial = 362880
sum digits = 27

'''

 



answered Feb 11, 2025 by avibootz
edited Feb 11, 2025 by avibootz
0 votes
def factorial(n):
    if n == 0 or n == 1:
        return 1
    else:
        return n * factorial(n - 1)

def sum_of_digits(number):
    return sum(int(digit) for digit in str(number))

def sum_of_factorial_digits(n):
    fact = factorial(n)
    
    return sum_of_digits(fact)


number = 9

print(f"The sum of the digits of {number}! is: {sum_of_factorial_digits(number)}")



'''
run:

The sum of the digits of 9! is: 27

'''

 



answered Feb 11, 2025 by avibootz

Related questions

1 answer 123 views
1 answer 132 views
1 answer 139 views
1 answer 113 views
1 answer 127 views
1 answer 128 views
1 answer 105 views
...