Skip to content
Related Articles

Related Articles

Improve Article

Java Program to find sum of array

  • Difficulty Level : Easy
  • Last Updated : 11 Mar, 2018

Given an array of integers, find sum of its elements.

Examples :

Input : arr[] = {1, 2, 3}
Output : 6
1 + 2 + 3 = 6

Input : arr[] = {15, 12, 13, 10}
Output : 50

Java




/* Java Program to find sum of elements in a given array  */
class Test
{
     static int arr[] = {12,3,4,15};
       
     // method for sum of elements in an array 
     static int sum()
     {
         int sum = 0; // initialize sum
         int i;
        
         // Iterate through all elements and add them to sum
         for (i = 0; i < arr.length; i++)
            sum +=  arr[i];
        
         return sum;
     }
       
     // Driver method
     public static void main(String[] args) 
     {
         System.out.println("Sum of given array is " + sum());
        }
 }

Please refer complete article on Program to find sum of elements in a given array for more details!

My Personal Notes arrow_drop_up
Recommended Articles
Page :