Write a program to find the sum of 1+ 1/8 + 1/27……1/n³, where n is the number input by the user.
python #Program to find the sum of series 1 + 1/8 + 1/27 + ... + 1/n³ n = int(input("Enter the value of n: ")) sum1 = 0 for i in range(1, n + 1): sum1 = sum1 + 1/(i**3) print("The sum of the series is:", sum1)
Output:
Enter the value of n: 3 The sum of the series is: 1.162037037037037
In this program, the user inputs the value of n which determines how many terms of the series will be added. The series follows the pattern where each term is 1 divided by the cube of the term number (i³). A for loop iterates from 1 to n, and in each iteration, the term 1/i³ is calculated and added to the variable sum1 which stores the cumulative sum. Finally, the total sum is printed.
Explanation
This question tests the student's understanding of loops and mathematical series computation. The series 1 + 1/8 + 1/27 + ... + 1/n³ can be expressed as Σ(1/i³) where i goes from 1 to n. The program uses a for loop with range(1, n+1) to iterate through each term, calculates 1/i³ using the exponentiation operator (**), and accumulates the sum. The context from Program 6-13 and Program 6-19 demonstrates similar patterns of using loops for summation and taking user input with int(input()).
Solution Steps
Step 1: Accept integer input n from user using int(input())
Step 2: Initialize sum1 = 0 to store the cumulative sum
Step 3: Use for loop with range(1, n+1) to iterate through each term
Step 4: Calculate each term as 1/(i**3) and add to sum1
Step 5: Print the final sum after the loop completes