Skip to content

Instantly share code, notes, and snippets.

@ghoomfrog
Created June 5, 2019 02:17
Show Gist options
  • Save ghoomfrog/83cf10281fea7994d3a931db5a4a1b83 to your computer and use it in GitHub Desktop.
Save ghoomfrog/83cf10281fea7994d3a931db5a4a1b83 to your computer and use it in GitHub Desktop.
Program to quickly sum integers from a start to an end.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
// if at least 2 arguments are given to the program
if (argc > 2) {
// first integer
long long f = atoll(argv[1]);
// last integer
long long l = atoll(argv[2]);
printf("%lli\n", (long long)((((l-f)+1)/2.0)*(f+l)));
}
// if 1 arguments is given to the program
else if (argc == 2) {
// last integer
long long l = atoll(argv[1]);
printf("%lli\n", (l/2)*(1+l));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment