Skip to content

Instantly share code, notes, and snippets.

@muhammadmuzzammil1998
Created September 15, 2017 14:47
Show Gist options
  • Save muhammadmuzzammil1998/555d7c316a2686820bbe06589cc3b0fd to your computer and use it in GitHub Desktop.
Save muhammadmuzzammil1998/555d7c316a2686820bbe06589cc3b0fd to your computer and use it in GitHub Desktop.
Carl Friedrich Gauss's anecdote of how he summed up 1 to 100 in an interesting manner using a for loop.
/*
* I was reading about Carl Friedrich Gauss's anecdote of how he summed up 1 to 100
* in an interesting manner. So, I wanted to make it with a loop (I know, I am a
* lazy guy) and came up with this pretty quickly... so quickly that I thought I did
* it wrong but the output was same as the great mathematician. But I think that
* there is some another and easy way to do this, right?
*/
#include <stdio.h>
int main() {
int i, j;
for (i = 0, j = 100; j > 0; i += j, j--);
printf("%d", i);
return 0;
}
// Output: 5050
@omern1
Copy link

omern1 commented Sep 15, 2017

So the point of the exercise?

@muhammadmuzzammil1998
Copy link
Author

nabeelomer: So the point of comment?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment