Java program which sums numbers from 1 to 100

前端 未结 5 1491
别那么骄傲
别那么骄傲 2021-01-29 16:50

I have been recently started learning Java and would want to make code that sums the numbers from 1 to 100 (result would be 5050)

The code should go like 1+2+3+4+5+6+7+8+

5条回答
  •  被撕碎了的回忆
    2021-01-29 17:11

    Hey you can find the sum from 1 to 100 like this also. Just a option.

    You can use the mathematical formula for sum of numbers i.e n(n+1)/2

    public class test {
    
        public static void main(String[] args) {
            int i =100;
            int sum = (i)*(i+1)/2;
            System.out.println(sum);
    
        }
    }
    

提交回复
热议问题