Problème
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).
Contraintes
0 ≤ n ≤ 1,000,000
Exemples
Exemple 1
Entrée :
n = 7
Sortie attendue :
expected = 16
Entrée :
long long n = 7;
Sortie attendue :
long long expected = 16;
Entrée :
long n = 7;
Sortie attendue :
long expected = 16;
Explication
1 + 3 + 5 + 7 = 16.
Exemple 2
Entrée :
n = 0
Sortie attendue :
expected = 0
Entrée :
long long n = 0;
Sortie attendue :
long long expected = 0;
Entrée :
long n = 0;
Sortie attendue :
long expected = 0;
Explication
There are no odd numbers from 1 to 0, so the sum is 0.
Références
Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.