You are here: Home / Topics / Program to get sum of elements of an array in Java

Program to get sum of elements of an array in Java

Filed under: Java on 2023-08-19 18:36:55

//  sum elements of array

public class SumOfElementsOfArray
{
   public static void main(String[] args)
   {
       int a[]=new int[5];
       int i,sum=0;
       a[0]=1;
       a[1]=2;
       a[2]=3;
       a[3]=4;
       a[4]=5;
       for(i=0;i<a.length;i++)
       {
           sum=sum+a[i];
       }
       System.out.print("Sum is : "+sum);
   }
}

 

Output:

Sum is : 15

About Author:
Y
Yatendra Sir     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.