Java Program to find the sum of array elements


In this program, You will learn how to find the sum of array elements in java.


List is: 10 20 30 40 50

150 = 10 + 20 + 30 + 40 + 50

Example: How to find the sum of array elements in java.

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("Sum of all elements:" + s);

    }
}

Output:

Enter 5 numbers:10 20 30 40 50
Sum of all elements:150

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.