In: Computer Science
given an array, write code to scan the array for a particular purpose . use java
This code for sum of an array
here i am taking first input for size of an aaray using scanner.
and then i am taking all element in the array using nextInt()
after all i am doing sum of all element here.
code:
import java.util.Scanner;
Public class sum_of_array
{
public static void main(String[] args)
{
int n,sum=0;
Scanner s=new Scanner(System.in);
System.out.println("Enter the size of an aaray-:");
n=nextInt[n];
int a[]=new int[n];
System.out.println("Enter all the element in array-:");
for(int i=0; i<n; i++)
{
a[i]=s.nextInt();
sum=sum+a[i];
}
System.out.println("Sum of array:"+sum);
}
}
Note:if there is any question in understanding kindly reach out to me in comment section