Skip to content

Instantly share code, notes, and snippets.

@kketernality
Last active September 15, 2019 14:22
Show Gist options
  • Save kketernality/3d4670f9b01df7db9b0f1dea09174a3d to your computer and use it in GitHub Desktop.
Save kketernality/3d4670f9b01df7db9b0f1dea09174a3d to your computer and use it in GitHub Desktop.
#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