Sum Of Array Elements

Sum Of Array Elements


Write a program to print the sum of all the elements of an array of size 'm'.

import java.io.*;

    class Array_Sum
    {
    public static void main ( String agrs[])throws IOException
    {
        BufferedReader obj= new BufferedReader(
            new InputStreamReader(System.in));
    int i,sum=0,m;
    boolean flag=false;

    System.out.println("Enter the length of the array : ");
     m=Integer.parseInt(obj.readLine());
     int a[]=new int[m];

    System.out.println("Enter "+m+" elements for the Ist array");

    for(i=0;i< m;i++)
    {
     a[i]=Integer.parseInt(obj.readLine());
    }

    for(i=0;i< m;i++)
    {
        sum+=a[i];
    }

    System.out.println("Sum of all the elements of the array : "+sum);

 }
}

Have something to say? Log in to comment on this post.

0 comments