Finding the sum of elements in an array is a basic and essential task in programming. It helps you understand array manipulation and traversal. Below is a detailed Java program to find the sum of elements in an array.
Java Program to Find the Sum of Elements in an Array
import java.util.Scanner;
public class SumOfElementsInArray {
public static void main(String[] args) {
// Create a Scanner object to read input from the user
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter the size of the array
System.out.print("Enter the size of the array: ");
int size = scanner.nextInt();
// Create an array of the given size
int[] array = new int[size];
// Prompt the user to enter the elements of the array
System.out.println("Enter the elements of the array:");
for (int i = 0; i < size; i++) {
array[i] = scanner.nextInt();
}
// Find the sum of elements in the array
int sum = findSumOfElements(array);
// Print the sum of elements
System.out.println("The sum of elements in the array is: " + sum);
}
// Method to find the sum of elements in an array
public static int findSumOfElements(int[] array) {
// Initialize sum to 0
int sum = 0;
// Traverse through the array and add each element to sum
for (int i = 0; i < array.length; i++) {
sum += array[i];
}
return sum;
}
}
Explanation of the Code
Java program to find the sum of elements in an array
Let’s break down the program step-by-step to understand how it works.
- Importing the Scanner Class: We import the
Scanner
class from thejava.util
package to read input from the user.
import java.util.Scanner;
2. Creating the Main Class: We create a public class named SumOfElementsInArray
. In Java, the class name should match the filename.
public class SumOfElementsInArray {
3. Main Method: The main
method is the entry point of the program. Inside this method, we will write the code to find the sum of elements in an array.
public static void main(String[] args) {
4. Creating a Scanner Object: We create a Scanner
object to read input from the user.
Scanner scanner = new Scanner(System.in);
5. Reading the Size of the Array: We prompt the user to enter the size of the array and store this value in a variable.
System.out.print("Enter the size of the array: ");
int size = scanner.nextInt();
6. Creating an Array: We create an array of the given size.
int[] array = new int[size];
7. Reading the Elements of the Array: We prompt the user to enter the elements of the array and store these values in the array.
System.out.println("Enter the elements of the array:");
for (int i = 0; i < size; i++) {
array[i] = scanner.nextInt();
}
8. Finding the Sum of Elements: We call the findSumOfElements
method to find the sum of elements in the array. The result is stored in a variable sum
.
int sum = findSumOfElements(array);
9. Printing the Sum of Elements: We print the sum of elements in the array.
Java program to find the sum of elements in an array
System.out.println("The sum of elements in the array is: " + sum);
10. Method to Find the Sum of Elements in an Array: The findSumOfElements
method finds the sum of elements in the given array. This method works by initializing a sum variable to 0 and then traversing through the array to add each element to the sum.
public static int findSumOfElements(int[] array) {
// Initialize sum to 0
int sum = 0;
// Traverse through the array and add each element to sum
for (int i = 0; i < array.length; i++) {
sum += array[i];
}
return sum;
}
Explanation of the findSumOfElements
Method
- Initializing the Sum Variable: We initialize the
sum
variable to 0.
int sum = 0;
2. Traversing the Array: The for
loop iterates through each element of the array. In each iteration, we add the current element to the sum
variable.
for (int i = 0; i < array.length; i++) {
sum += array[i];
}
3. Returning the Sum: After the loop finishes, we return the sum
variable, which now holds the sum of all elements in the array.
return sum;
Running the Program
To run the program, follow these steps:
- Save the code in a file named
SumOfElementsInArray.java
. - Open a command prompt or terminal and navigate to the directory where you saved the file.
- Compile the program using the following command:
javac SumOfElementsInArray.java
4. Run the compiled program using the following command:
java SumOfElementsInArray
5.
- Follow the prompts to enter the size and elements of the array. The program will display the sum of elements in the array.
Example Output
If you run the program and enter the following input:
Enter the size of the array: 5
Enter the elements of the array:
10
45
32
67
29
The output will be:
The sum of elements in the array is: 183
What did we learn from this article?
Java program to find the sum of elements in an array. Finding the sum of elements in an array is a basic yet important exercise that helps understand array manipulation and traversal in Java. This Java program demonstrates how to find the sum of elements in an array by iterating through the array and adding each element to the sum. By practicing such basic programs, you can strengthen your problem-solving skills and gain confidence in your ability to write efficient Java code. Keep practicing and exploring more problems to enhance your programming skills. Java program to find the sum of elements in an array