Java: Array with loop

后端 未结 9 1044
借酒劲吻你
借酒劲吻你 2020-12-06 04:49

I need to create an array with 100 numbers (1-100) and then calculate how much it all will be (1+2+3+4+..+100 = sum).

I don\'t want to enter these numbers into the a

9条回答
  •  遥遥无期
    2020-12-06 05:08

    If your array of numbers always is starting with 1 and ending with X then you could use the following formula: sum = x * (x+1) / 2

    from 1 till 100 the sum would be 100 * 101 / 2 = 5050

提交回复
热议问题