We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
def sumofodd(n):
a = 0 # Initialize the accumulator
for i in range(1, n+1): # Loop through numbers from 1 to n
if i % 2 != 0: # Check if the number is odd
a += i # Add the odd number to a
return a
b = sumofodd(5)
print(b)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sum of Odd Elements
You are viewing a single comment's thread. Return to all comments →
def sumofodd(n): a = 0 # Initialize the accumulator for i in range(1, n+1): # Loop through numbers from 1 to n if i % 2 != 0: # Check if the number is odd a += i # Add the odd number to a return a
b = sumofodd(5) print(b)