Sum of any number which are in sequence from start to end , there is a mathematical formula to calculate the sum of the given number if they are in sequence is

**sum=n*** (Fisrt_Number+Last_Number)/2 , *where n is the count of given numbers for example: sum of 20, 21, 22, 23, 24, 25 can be calculated as:

*sum=6*(20+25)/2 =135 , here n is 6 which is total given number’s, below a java program is written to calculate the same using the given formula.

int totalSum = length * (firstNum + lastNum) / 2;

package com.il.basics.programs; import java.util.Arrays; public class SumOfNConsecutiveNumbers { public static void main(String[] args) { int array[] = { 20, 21, 22, 23, 24, 25 }; int length = array.length; int firstNum = array[0]; int lastNum = array[length - 1]; int totalSum = length * (firstNum + lastNum) / 2; System.out.println("Sum of " + Arrays.toString(array) + " is: " + totalSum); } } ******output******* Sum of [20, 21, 22, 23, 24, 25] is: 135