Code Explanation:
Initialize the number
n = 7; s = 0
n stores the limit (up to 7)
s is used to store the sum
Initially, sum is set to 0
Start the loop
for i in range(1, n+1, 2):
Loop starts from 1
Ends at n (7)
Step value 2 means only odd numbers are selected
(1, 3, 5, 7)
Add odd numbers
s += i
Each odd number is added to s
Equivalent to s = s + i
Print the result
print(s)
Displays the final sum of odd numbers
✅ Final Output
16

0 Comments:
Post a Comment