Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@nabeelomer

This comment has been minimized.

Copy link

@nabeelomer nabeelomer commented Sep 15, 2017

So the point of the exercise?

@muhammadmuzzammil1998

This comment has been minimized.

Copy link
Owner Author

@muhammadmuzzammil1998 muhammadmuzzammil1998 commented Sep 15, 2017

nabeelomer: So the point of comment?

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