โจทย์
Add all odd numbers from 1 up to and including n, using a loop.
Implement sum_of_odds(n).
Input
n: The upper limit.
Output
Return the sum of the odd numbers from 1 through n (0 if there are none).
เงื่อนไข
0 ≤ n ≤ 1,000,000
ตัวอย่าง
ตัวอย่างที่ 1
อินพุต:
n = 7
ผลลัพธ์ที่คาดหวัง:
expected = 16
อินพุต:
long long n = 7;
ผลลัพธ์ที่คาดหวัง:
long long expected = 16;
อินพุต:
long n = 7;
ผลลัพธ์ที่คาดหวัง:
long expected = 16;
คำอธิบาย
1 + 3 + 5 + 7 = 16.
ตัวอย่างที่ 2
อินพุต:
n = 0
ผลลัพธ์ที่คาดหวัง:
expected = 0
อินพุต:
long long n = 0;
ผลลัพธ์ที่คาดหวัง:
long long expected = 0;
อินพุต:
long n = 0;
ผลลัพธ์ที่คาดหวัง:
long expected = 0;
คำอธิบาย
There are no odd numbers from 1 to 0, so the sum is 0.
แหล่งอ้างอิง
โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน