Program to find sum of 1 + x / 2 ! + x ^ 2 / 3 ! + ... + x ^ n / ( n + 1 ) ! | Function to find the factorial of a number ; Function to compute the sum ; Iterate the loop till n and compute the formula ; Get x and n ; Print output
function fact ( n ) { if ( n == 1 ) return 1 ; return n * fact ( n - 1 ) ; } function sum ( x , n ) { let total = 1.0 ; for ( let i = 1 ; i <= n ; i ++ ) { total = total + ( Math . pow ( x , i ) / fact ( i + 1 ) ) ; } return total . toFixed ( 4 ) ; } let x = 5 ; let n = 4 ; document . write ( " " + sum ( x , n ) ) ;