Home > front end >  factorial calculation by user defining function in python
factorial calculation by user defining function in python

Time:12-11

Write a program to find out the result of the expression given below using user defined function(factorial())

expression: 1 x^2 x^2/2! x^2/3! ..... x^2/n!

def factorial(n,x):
    sum=1
    for i in range(1,n 1):
        sum =(x**2/i)
    print("{0:.2f}".format(sum))
n,x=int(input("n: ")),int(input("x: "))
factorial(n,x)

Not getting answer.

CodePudding user response:

import math
def factorial(n,x):
    sum=1
    for i in range(1,n 1):
        sum =(x**2)/math.factorial(i)
    print("{0:.2f}".format(sum))
n,x=int(input("n: ")),int(input("x: "))
factorial(n,x)

CodePudding user response:

import math
def factorial(n,x):
    sum=1
    for i in range(1,n 1):
        sum =(x**2)/math.factorial(i)
    print("{0:.2f}".format(sum))
n,x=int(input("n: ")),int(input("x: "))
factorial(n,x)
  • Related