Java Number Sum sum0ToN(int n)

Here you can find the source of sum0ToN(int n)

Description

Returns the sum of integers from 0 up to n.

License

Open Source License

Declaration

public static int sum0ToN(int n) 

Method Source Code

//package com.java2s;
// it under the terms of the GNU General Public License as published by      //

public class Main {
    /**//from  w  w w  .ja va2  s.  c  o m
     * Returns the sum of integers from 0 up to n.
     */
    public static int sum0ToN(int n) {
        if (n < 0) {
            throw new IllegalArgumentException("Argument must be >= 0: " + n);
        }

        return n * (n + 1) / 2;
    }
}

Related

  1. sum(long x, int c)
  2. sum(Number n1, Number n2)
  3. Sum(Object in, int min, int max)
  4. sum(String fieldName)
  5. sum(String str11, String str22)
  6. sum_quad(long max)
  7. sum_quad_endrec(long max)
  8. sumaMesFull(String sF, int n)
  9. sumAsDouble(Iterable values)