write python code to add the odd numbers up to (and including) a given value N and print the result.
Answers
Answered by
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
3
Answer:
refer the attachment ☝☝
Attachments:
![](https://hi-static.z-dn.net/files/d47/6e94c6b957a4a72f5c272d58365861e6.jpg)
![](https://hi-static.z-dn.net/files/da8/390a2615de639845cbf3e1cffbd3e789.jpg)
Similar questions
Geography,
6 months ago
Social Sciences,
6 months ago
Math,
6 months ago
Math,
1 year ago
History,
1 year ago