Last active
September 15, 2019 14:22
-
-
Save kketernality/3d4670f9b01df7db9b0f1dea09174a3d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdlib> | |
bool isEven(int n) | |
{ | |
return n % 2 == 0; | |
} | |
// Note the computation order of operator | |
int computeSumToNumber(int n) | |
{ | |
return (n * (n + 1)) / 2; | |
} | |
int main(int argc, char* argv[]) | |
{ | |
int n = -1; | |
// Only even number is allowed | |
while (!isEven(n)) scanf("%d", &n); | |
printf("%d", computeSumToNumber(n / 2)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment