Music, asked by Anonymous, 1 year ago

write python code to add the odd numbers up to (and including) a given value N and print the result.​

Answers

Answered by Equestriadash
39

Source code:

n = int(input("Enter a number: "))

s = 0

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

   if i%2 != 0:

       s = s + i

print(s, "is the sum of all the ODD numbers from 0 to", n)

Once the user inputs a number, a loop is started.

In the loop, the given range is from 0 to the number input by the user. We added the end value by 1 since for loops don't consider the end value while traversing.

After that, we use an if condition to test if the number is odd or not. If it is, it moves on to the next expression, which is the sum.

Answered by atulchaurasia23
3

Answer:

refer the attachment ☝☝

Attachments:
Similar questions