sum of array elements in java

import java.util.Scanner;
public class Sum {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int sum=0;
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
sum=sum+a[i];
}
System.out.println(sum);
}

}

i/p: 5
1 2 4 5 6
o/p: 18

No comments:

Post a Comment