Ask Question
13 February, 14:01

Write a function "sumsteps2" that calculates and returns the sum of 1 to n in increments of 2, where n is an argument passed to the function. For example, if 11 is passed, it will return the sum of 1 + 3 + 5 + 7 + 9 + 11. Do this using a for loop

+3
Answers (1)
  1. 13 February, 17:23
    0
    def sumsteps2 (n):

    sum = 0

    if n % 2 = = 1:

    n + = 1

    for i in range (1, n+1, 2):

    sum + = i

    return sum

    print ("Sum is: " + str (sumsteps2 (11)))

    Explanation:

    - Create a function called sumsteps2 () that takes one parameter, n

    - Initialize the sum as 0

    - Check if n is odd. If it is odd, increment the n by 1, to make it inclusive

    - Initialize a for loop that iterates from 1 to n in increments of 2

    - Add the numbers to the sum

    - Return the sum

    - Call the function to see the result
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a function "sumsteps2" that calculates and returns the sum of 1 to n in increments of 2, where n is an argument passed to the ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers