Einfach 0 gelöst 0% Erfolgsquote

Sum of Odd Numbers

Add every odd number up to a limit.

Problem

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).

Einschränkungen

0 ≤ n ≤ 1,000,000

Beispiele

Beispiel 1
Eingabe:
n = 7

Erwartete Ausgabe:
expected = 16
Eingabe:
long long n = 7;

Erwartete Ausgabe:
long long expected = 16;
Eingabe:
long n = 7;

Erwartete Ausgabe:
long expected = 16;

Erläuterung

1 + 3 + 5 + 7 = 16.

Beispiel 2
Eingabe:
n = 0

Erwartete Ausgabe:
expected = 0
Eingabe:
long long n = 0;

Erwartete Ausgabe:
long long expected = 0;
Eingabe:
long n = 0;

Erwartete Ausgabe:
long expected = 0;

Erläuterung

There are no odd numbers from 1 to 0, so the sum is 0.

Referenzen

Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.

Von anderen lernen

Community-Lösungen

Noch keine veröffentlichten Lösungen. Löse die Aufgabe und teile als Erster eine Lösung.