Java Program to Find the Sum of Elements in an Array

Introduction

Finding the sum of all elements in an array is a basic operation that is commonly used in various programming tasks. This guide will walk you through writing a Java program that calculates the sum of the elements in a given array.

Problem Statement

Create a Java program that:

  • Prompts the user to enter the size of an array and its elements.
  • Calculates the sum of all the elements in the array.
  • Displays the sum.

Example:

  • Input: [1, 2, 3, 4, 5]
  • Output: "Sum of elements: 15"

Solution Steps

  1. Read the Array Size and Elements: Use the Scanner class to take the size and elements of the array as input from the user.
  2. Initialize a Sum Variable: Initialize a variable to store the sum of the elements.
  3. Traverse the Array: Use a loop to iterate through the array and accumulate the sum of its elements.
  4. Display the Result: Print the sum of the elements.

Java Program

// Java Program to Find the Sum of Elements in an Array
// Author: https://www.rameshfadatare.com/

import java.util.Scanner;

public class SumOfArrayElements {
    public static void main(String[] args) {
        // Step 1: Read the size and elements of the array from the user
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("Enter the size of the array: ");
        int size = scanner.nextInt();
        
        int[] array = new int[size];
        
        System.out.println("Enter the elements of the array:");
        for (int i = 0; i < size; i++) {
            array[i] = scanner.nextInt();
        }
        
        // Step 2: Initialize sum variable
        int sum = 0;
        
        // Step 3: Traverse the array and calculate the sum
        for (int i = 0; i < size; i++) {
            sum += array[i];
        }
        
        // Step 4: Display the sum of elements
        System.out.println("Sum of elements: " + sum);
    }
}

Explanation

Step 1: Read the Array Size and Elements

  • The Scanner class is used to read the size of the array and its elements. The nextInt() method captures the size and each element.

Step 2: Initialize a Sum Variable

  • A variable sum is initialized to 0. This variable will be used to accumulate the sum of the array elements.

Step 3: Traverse the Array

  • A for loop is used to iterate through each element of the array.
  • During each iteration, the value of the current element is added to sum.

Step 4: Display the Result

  • The program prints the total sum of the elements using System.out.println().

Output Example

Example 1:

Enter the size of the array: 5
Enter the elements of the array:
1 2 3 4 5
Sum of elements: 15

Example 2:

Enter the size of the array: 3
Enter the elements of the array:
10 20 30
Sum of elements: 60

Example 3:

Enter the size of the array: 6
Enter the elements of the array:
5 10 15 20 25 30
Sum of elements: 105

Conclusion

This Java program demonstrates how to calculate the sum of elements in an array. It covers basic concepts such as loops, array traversal, and arithmetic operations, making it a valuable exercise for beginners learning Java programming.

Leave a Comment

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

Scroll to Top