Signup/Sign In

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

  1. Start
  2. Declare the array size.
  3. Ask the user to initialize the array size.
  4. Declare the array.
  5. Ask the user to initialize the array elements.
  6. Declare a variable sum to store the sum of all the elements in the array.
  7. Initialize the variable to 0.
  8. Using a for-each loop calculates the sum of all the elements in the array.
  9. Display the sum.
  10. 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

  1. Start
  2. Declare the array size.
  3. Ask the user to initialize the array size.
  4. Declare the array.
  5. Ask the user to initialize the array elements.
  6. Declare a variable sum to store the sum of all the elements in the array.
  7. Initialize the variable to 0.
  8. Declare another variable to iterate through all the elements of the array.
  9. Initialize it to 0.
  10. Using a while loop calculates the sum of all the elements in the array.
  11. Increment the value of the sum in each iteration.
  12. Increment the value of the other variable in each iteration to traverse through all the elements.
  13. Display the sum.
  14. 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



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.