Addition of all integer numbers from 1 - 100 (loop)

hey guys,

I am an absolutely beginner who wants to add all integer numbers from 1 to 100 ( in a loop) and print the result with println(). I know that I should use a for() loop but how can I add each element of 1-100 in order to get the sum?

like this : 0+1+2+3+4+5+6 …+100 = …?

thanks for your help!

HI @hullah
See the documents
https://processing.org/reference/addassign.html

int sum;

void setup() { 
  for (int i = 0; i <= 100; i++) {
    sum += i;
  }
  println(sum);
}

thanks a lot noel! :slight_smile:

if you need an equasion for it use

int n = 5;
println(  ( n*(n+1)/2 ));

n ( n + 1 ) / 2 = ( n^2 + n ) / 2

its a simple way to do it

1 Like

that’s great! thanks !

Hello,

Additional references:

:)