BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java program to sum the elements of an array

Last Updated: September 10, 2022 by Chaitanya Singh | Filed Under: Java Examples

In this tutorial we will see how to sum up all the elements of an array.

Program 1: No user interaction

/**
 * @author: BeginnersBook.com
 * @description: Get sum of array elements
 */
class SumOfArray{
   public static void main(String args[]){
      int[] array = {10, 20, 30, 40, 50, 10};
      int sum = 0;
      //Advanced for loop
      for( int num : array) {
          sum = sum+num;
      }
      System.out.println("Sum of array elements is:"+sum);
   }
}

Output:

Sum of array elements is:160

Program 2: User enters the array’s elements

/**
 * @author: BeginnersBook.com
 * @description: User would enter the 10 elements
 * and the program will store them into an array and 
 * will display the sum of them.
 */
import java.util.Scanner;
class SumDemo{
   public static void main(String args[]){
      Scanner scanner = new Scanner(System.in);
      int[] array = new int[10];
      int sum = 0;
      System.out.println("Enter the elements:");
      for (int i=0; i<10; i++)
      {
    	  array[i] = scanner.nextInt();
      }
      for( int num : array) {
          sum = sum+num;
      }
      System.out.println("Sum of array elements is:"+sum);
   }
}

Output:

Enter the elements:
1
2
3
4
5
6
7
8
9
10
Sum of array elements is:55

Top Related Articles:

  1. Java Program to Calculate average using Array
  2. Neon Number in Java with example
  3. Java program to print number of elements in an array
  4. Sunny Number Program in Java
  5. Java Program to right rotate the elements of an array

Tags: Java-Array

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Karen Mmrdl says

    September 18, 2015 at 4:17 PM

    what is ” for( int num : array) “?

    Reply
    • Kumar Abhisek says

      October 2, 2015 at 8:33 AM

      It is For Each Loop or enhanced for loop introduced in java 1.7

      For (int num : array )
      Here int is data type for num variable where you want to store all arrays data in otherwords you can say the destination where you want to give all component of arrays.

      Here array is the name of the array itself. So all arrays components we are giving to num variable.

      Reply
    • sam says

      October 4, 2015 at 2:35 PM

      its for each loop it take each element from array and assign to num.

      Reply
    • roma says

      October 8, 2015 at 12:49 PM

      it is a variation of for loop
      basically used in collections
      but you can use it to iterate through elements…
      hope you get it…

      Reply
  2. Suman says

    April 13, 2016 at 4:06 PM

    If there are more than one array in program how for each loop distinguish each of them , doubt is how to use more than one for each loop in one prog having different different array list?

    Reply
  3. muhammad says

    June 17, 2016 at 10:54 PM

    How do I compute total sum of values in a row in a non uniform array

    Reply
  4. akram says

    July 4, 2016 at 9:08 AM

    how to print average of add numbers in given array int arr[] = {12,22,33,1,-2,3,8,11,9} in java?

    Reply
    • Tarun Goyal says

      August 10, 2016 at 10:01 AM

      public class AverageIntArray {

      public static void main(String[] args) {
      int arr[] = {12,22,33,1,-2,3,8,11,9};
      float average=0;

      float sum = 0;
      int length = arr.length;
      for (int num : arr){
      sum = sum + num;
      }

      average = sum/length;

      System.out.println(average);

      }

      }

      Reply
  5. Darshana says

    December 31, 2016 at 3:52 PM

    how to find sum of first 5 elements of an array{1,2,3,4,5,6,7,8,9,10} in java using for-each?

    Reply
  6. nihal says

    June 19, 2017 at 1:48 PM

    How to write to finding sum of two 1d array with size of both array enter by the user as like …
    Enter size of array :2
    Enter element of first array
    Stored as
    X[0]=1
    X[1]=2
    Enter element of second array
    Y[0]=3
    Y[1]=6
    Print shows as z[0]=x[0]+y[0]
    And z[1]=x[1]+Y[1]

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap