JAVA Program for calculating the sum of all the elements of an array
Sum of all the elements of an array
In this section, we learn about Program for calculating the sum of all the elements of an array in java programming language. We are given with an array and need to find the sum of the given elements of the array. We will discuss various approaches to find the solution of the given problem.
Here, we will discuss different methods to find the required sum of the elements of the array. Different methods discussed on this page are :
- Method 1 : Using Iteration
- Method 2 : Using recursion (Top-Down Approach)
- Method 3 : Using recursion (Bottom-up Approach)
Method 1 :
- Create a variable say sum = 0.
- Run a loop to iterate over the entire array.
- Set sum = sum + arr[i]
- After complete iteration print(sum)
Method 1 : Java Code
Run
import java.util.Scanner; public class Main { public static void main(String args[]) { int arr[] = {12, 13, 1, 10, 34, 10}; int sum = 0; for(int i=0; i<arr.length; i++) { sum = sum + arr[i]; } System.out.print(sum); } }
Output :
80
Method 2 :
- Declare a recursive function say getSum(int arr[], int index, int len)
- If(index==len-1) return arr[index]
- Otherwise, return (arr[index] + getSum(arr, index+1, len))
Method 2 : Java Code
Run
import java.util.Scanner; public class Main { static int getSum(int arr[], int index, int len){ if(index==len-1) return arr[index]; return arr[index] + getSum(arr, index+1, len); } public static void main(String args[]) { int arr[] = {12, 13, 1, 10, 34, 10}; int n = arr.length; System.out.print(getSum(arr, 0, n)); } }
Output :
80
Method 3 :
- Create a recursive function say getSum(int arr[], int index)
- If(index==0) return arr[index]
- Otherwise, return (arr[index] + getSum(arr, index-1))
Method 3 : Java Code
Run
import java.util.Scanner; public class Main { static int getSum(int[] arr, int index){ if(index==0) return arr[index]; return arr[index] + getSum(arr, index-1); } public static void main(String args[]) { int arr[] = {12, 13, 1, 10, 34, 10}; int n = arr.length; System.out.print(getSum(arr, n-1)); } }
Output :
80
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment
import java.util.*;
class HelloWorld {
static int sumNum(int[] a,int n){
if(n==0){
return a[n];
}
return a[n]+sumNum(a,n-1);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
System.out.print(sumNum(a,n-1));
}
}
Join our Discord channel for all your technical queries.
import java.util.*;
public class sum_of_all_the_elements_of_an_array {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.print(“Size : “);
int size=in.nextInt();
System.out.println(“Numbers are “);
int[]a=new int[size];
int sum=0;
for(int i=0;i<size;i++)
{
a[i]=in.nextInt();
sum=sum+a[i];
}
System.out.print("Sum of elements is "+sum);
}
}
import java.util.Scanner;
class Ram {
void Disp() {
Scanner sc = new Scanner(System.in);
int n, sum = 0;
System.out.println(“Enter the Size :”);
n = sc.nextInt();
int a[] = new int[n];
System.out.println(“Enter the array elements:”);
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
System.out.println("Array elements are:");
for (int i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
for (int i = 0; i < n; i++) {
sum = sum + a[i];
}
System.out.println("Sum of elements are " + sum);
}
}
public class SumOfElement {
public static void main(String args[]) {
Ram r = new Ram();
r.Disp();
}
}