(Python Programming)
Write a program that prompts a user for a positive integer and then uses a loop to calculate and display the sum of specific fractions as follows:
Let's say the user enters 5, then your program will compute: 1/5 + 2/4 + 3/3 + 4/2 + 5/1 which is 8.7.
CODE -
# Taking a number from user as input
num = int(input("Enter a number: "))
# Initializing the sum of specific fractions to 0
sum_frac = 0
# Calculating the sum of specific fractions using for loop
for i in range(1, num+1):
sum_frac += (i / (num-i+1))
# Displaying the sum of specific fractions.
print(sum_frac)
SCREENSHOT -
If you have any doubt regarding the solution, then do
comment.
Do upvote.
Get Answers For Free
Most questions answered within 1 hours.