Alice is working on a mathematical series for her project and needs to calculate the sum of the series where each term is the ratio of the integer to its factorial up to a given number n. Write a program to help Alice by taking an integer n as input and printing the sum of the series, formatted to two decimal places.Company Tags: InfosysInput format :The input consists of an integer n.Output format :The output prints a double value, which is the sum of the series, rounded off to two decimal places.Refer to the sample output for formatting specifications.Code constraints :1 ≤ n ≤ 8Sample test cases :Input 1 :5Output 1 :2.71Input 2 :2Output 2 :2.00
Question
Alice is working on a mathematical series for her project and needs to calculate the sum of the series where each term is the ratio of the integer to its factorial up to a given number n. Write a program to help Alice by taking an integer n as input and printing the sum of the series, formatted to two decimal places.Company Tags: InfosysInput format :The input consists of an integer n.Output format :The output prints a double value, which is the sum of the series, rounded off to two decimal places.Refer to the sample output for formatting specifications.Code constraints :1 ≤ n ≤ 8Sample test cases :Input 1 :5Output 1 :2.71Input 2 :2Output 2 :2.00
Solution 1
Here is a Python program that can help Alice calculate the sum of the series:
import math
def calculate_sum(n):
sum = 0.0
for i in range(1, n+1):
sum += i/math.factorial(i)
return round(sum, 2)
n = int(input())
print("{:.2f}".format(calculate_sum(n)))
This program
Solution 2
Here is a Python program that can help Alice calculate the sum of the series:
import math
def calculate_sum(n):
sum = 0.0
for i in range(1, n+1):
sum += i/math.factorial(i)
return round(sum, 2)
n = int(input())
print("{:.2f}".format(calculate_sum(n)))
This program works as follows:
-
It defines a function
calculate_sum(n)that calculates the sum of the series up ton. Inside this function, it initializes a variablesumto 0.0. -
Then it runs a loop from 1 to
n(inclusive). For each integeriin this range, it adds the ratio ofito its factorial tosum. The factorial is calculated using themath.factorial(i)function. -
The function then returns the sum rounded to two decimal places using the
round(sum, 2)function. -
The main part of the program reads an integer
nfrom the input, callscalculate_sum(n), and prints the result formatted to two decimal places. The formatting is done using the"{:.2f}".format(calculate_sum(n))expression.
Similar Questions
Write a program to calculate the factorial of N.
Write a program that takes a positive integer as input and calculates its factorial using a for loop. The factorial of a number is the product of all positive integers up to that number.
James, a mathematics teacher, is developing a programming exercise to help his students practice continuously summing the digits of a number until it becomes a single-digit integer. He wants to create a simple program using a 'while' loop that takes a positive integer input and generates the final single-digit result. Input format :The input consists of a positive integer n.Output format :The output prints "The single-digit sum of n is Y." where n is the input integer and Y is its single-digit sum.Refer to the sample output for formatting specifications.Code constraints :1 ≤ n ≤ 1010Sample test cases :Input 1 :456Output 1 :The single-digit sum of 456 is 6.Input 2 :999Output 2 :The single-digit sum of 999
Input format :The input consists of a single integer, n.Output format :The output displays the sum of consecutive numbers from 1 to n (exclusive), doubling each number before adding.
Write a Python program to calculate the sum of the series: 1 + 2 + 3 + … + n. e.g.:Input: 5Output: 15CODE EDITORCONSOLE12345678n = input()def series_sum(num): print(series_sum(n))
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.