LAST UPDATED: MARCH 5, 2021
Java Program To Accept Array Elements and Calculate the Sum
In this tutorial, we will learn how to accept array elements and calculate the sum. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java.
Input: 91 72 63 54 91 21 43 45 64 40
Output: The sum of all the elements in the array is 584
Program 1: Calculate the sum of Array Elements
In this method, we will see how to accept the elements of the array and calculate the total sum of all the elements in the array using a for-each loop.
Algorithm
- Start
- Declare the array size.
- Ask the user to initialize the array size.
- Declare the array.
- Ask the user to initialize the array elements.
- Declare a variable sum to store the sum of all the elements in the array.
- Initialize the variable to 0.
- Using a for-each loop calculates the sum of all the elements in the array.
- Display the sum.
- Stop.
The below program demonstrates how to accept the elements of an array and calculate the sum of all the elements in the array using each loop.
/*Java Program to find the sum of all the elements in the array using */
import java.util.*;
import java.util.Arrays;
//Driver Code
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n; //Declare array size
System.out.println("Enter the total number of elements ");
n=sc.nextInt(); //Initialize array size
int arr[]=new int[n]; //Declare the array
System.out.println("Enter the elements of the array ");
for(int i=0; i<n ;i++) //Initialize the array
{
arr[i]=sc.nextInt();
}
int sum = 0; //Variable to calculate the total sum
//Using For each loop
for( int num : arr)
{
sum = sum+num; //Increment the value of sum in each iteration
}
//Print the total sum
System.out.println("The sum of all the elements in the array is "+sum);
}
}
Enter the total number of elements 10
Enter the elements of the array 98 71 62 55 34 21 90 73 21 32
The sum of all the elements in the array is 557
Program 2: Calculate the Sum of Array Elements
In this method, we will see how to accept the elements of the array and calculate the total sum of all the elements in the array using a while loop.
Algorithm
- Start
- Declare the array size.
- Ask the user to initialize the array size.
- Declare the array.
- Ask the user to initialize the array elements.
- Declare a variable sum to store the sum of all the elements in the array.
- Initialize the variable to 0.
- Declare another variable to iterate through all the elements of the array.
- Initialize it to 0.
- Using a while loop calculates the sum of all the elements in the array.
- Increment the value of the sum in each iteration.
- Increment the value of the other variable in each iteration to traverse through all the elements.
- Display the sum.
- Stop.
The below program demonstrates how to accept the elements of an array and calculate the sum of all the elements in the array using a while loop.
/*Java Program to find the sum of all the elements in the array*/
import java.util.*;
import java.util.Arrays;
//Driver Code
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n; //Declare array size
System.out.println("Enter the total number of elements ");
n=sc.nextInt(); //Initialize array size
int arr[]=new int[n]; //Declare array
System.out.println("Enter the elements of the array ");
for(int i=0; i<n ;i++) //Initialize array
{
arr[i]=sc.nextInt();
}
int sum = 0; //Variable to store the sum
//Using while loop calculate the sum
int i=0; //Variable to iterate through all the elements
while(i!=n)
{
sum=sum+arr[i]; //Increment the value of sum in each iteration
I++; //Increment to iterate to the next element
}
//Print the sum
System.out.println("The sum of all the elements in the array is "+sum);
}
}
Enter the total number of elements 10
Enter the elements of the array 9 7 6 5 91 21 43 45 64 40
The sum of all the elements in the array is 331