Java Program to find sum of array elements


In this program, You will learn how to find sum of array elements in Java


List is: 10 20 30 40 50

150 = 10 + 20 + 30 + 40 + 50


Example: How to find sum of array elements in Java

Run it Now
import java.util.Scanner;

class Main {
    public static void main(String args[]) {

        int i, s = 0;
        int arr[] = new int[5];
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter 5 Numbers :");

        for (i = 0; i < 5; i++) {
            arr[i] = sc.nextInt();
        }

        /* Sum of Array */
        for (i = 0; i < 5; i++) {
            s = s + arr[i];
        }

        System.out.println("\nSum of All Elements is :" + s);

    }
}

Output:

Enter 5 Numbers :10 20 30 40 50
Sum of All Elements is :150