Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: Factorial digit sum : Problem 20 Euler Project Pyhton Code

Saturday, August 30, 2014

Factorial digit sum : Problem 20 Euler Project Pyhton Code

Factorial digit sum : Problem 20 Euler Project

n! means n × (n − 1) × ... × 3 × 2 × 1
For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
Find the sum of the digits in the number 100!


Python Code

def main():
    x=input("Enter a natural number:")
    x1 = x
    l=1
    for i in range(x):
        l=l*x
        x=x-1
    print l
    strNumb = str(l)
    sumFact = 0
    for i in range(len(strNumb)):
        sumFact = sumFact + int(strNumb[i])
    print "sum of the digits in the number",x1,"! is",sumFact
main()

No comments:

Post a Comment