Chapter #6 – Flow of Control

Class 11 – Chapter 6 Solutions – PROGRAMMING EXERCISES #6
Write a program to find the sum of 1+ 1/8 + 1/27……1/n3, where n is the number input by the user.
Python Program:
def sum_of_series(n):
total_sum = 0.0
for i in range(1, n + 1):
total_sum += 1 / (i ** 3)
return total_sum
# Get user input
try:
n = int(input("Enter a positive integer value for n: "))
if n <= 0:
print("Please enter a positive integer.")
else:
result = sum_of_series(n)
print(f"The sum of the series up to n={n} is: {result:.6f}")
except ValueError:
print("Invalid input. Please enter an integer.")
You can checkout the code from Github here – Programming Exercises – 6
Explanation of the Code:
1. Function Definition: The sum_of_series function takes an integer n as
input and initializes total_sum to store the cumulative sum.
2. Loop: It iterates from 1 to n, calculating each term as 1 / i3 and adding
it to total_sum.
3. User Input: The program prompts the user to enter a positive integer value
for n. It checks if the input is valid and positive.
4. Output: If valid, it calls the function and prints the calculated sum
formatted to six decimal places.
Example Output:
Enter a positive integer value for n: 5
The sum of the series up to n=5 is: 1.185662
NCERT Computer Science Class 11 Chapter 6 Solutions Q 11