155k views
3 votes
Question 2: Sum of consecutive odd View Past Answers No past answers. Write a program which asks user to enter an integer n, calculates the total sum of the consecutive ODD integers from 1 to n (inclusive of n if n is odd) and store in the variable called sum calculates the average value of the consecutive ODD integers from 1 to n and store in the variable called average prints the value of sum and average For example if n = 7, the program will compute 1+3+5+7, sum = 16, average = 4 if n = 6, the program will compute 1+3+5, sum = 9, average = 3

Pls help ya thx(btw this is python)

User Tarn
by
6.5k points

1 Answer

5 votes

Answer:

number = int(input("Enter number ") )

sum = 0

count = 0

for x in range(1, number+1, 2):

sum += x

count += 1

average = sum/count

print("sum = %d"% sum)

print("average = %d"% average)

The range() function conveniently lets you enumerate all odd values from 1 to your number.

User MADFROST
by
5.6k points