accept n elements from keyboard and perform the addition of all the entered elements in java

Write a java program to accept n elements from keyboard and perform the addition of all the entered elements.

package array;

import java.util.*;
public class sumofarray 
{
    public static void main(String args[])
    {
        Scanner s=new Scanner(System.in);
        int size,sum=0;
        System.out.println("enter size");
        size=s.nextInt();
        int a[]=new int[size];
        for(int i=0;i<size;i++)
        {
          a[i]=s.nextInt();
          sum=sum+a[i];
        }
        for(int i=0;i<size;i++)
        {
            System.out.println("elemnts are::"+a[i]);
        }
        System.out.println("Sum of elements is:"+sum);
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *