Java Program to find the sum of array elements.
Get array size n and n elements of array, then compute sum of the elements.
Sample Input 1:
5 5 7 9 3 1
Sample Output 1:
Program or Solution
Array is a Collection of data with same type.
1. Get the size of the Array
2. Create a array with the given size (Array has 0 to size-1 index to access every location)
0 1 2 3 ...... size-2 size-1
Here i starts at 0, incremented by 1 at every iteration and finally iteration stops when i is equal to size.
In First iteration i is 0, so a[i] is a
In second iteration i is 1, so a[i] is a
In last iteration i is size-1, so a[i] is a[size-1]
the input statement nextInt() reads input and stores in array location consecutively. like a, a, a.....
The second For Loop
sums the values of array consecutively like
sum = sum + a[i]
sum = sum + a in first iteration
sum = sum + a in second iteration
sum = sum + a[size-1] in last iteration
Finally prints the value of sum.
- Java Program to get and print the array elements
- Java Program to find the average of array Elements
- Java Program to Search an Element in an array
- Java Program to find Largest element in the array
- Java Program to find Smallest element in the array
- Java Program to print all the numbers which are less than given key element from a given array.
- Java Program to Sort the Elements in ascending order.
- Java Program to Sort the Elements in descending order.
- Addition of Matrix in Java
- Java Program to delete an Element from the Array
- Java Program to merge two Arrays
- Java Program to Multiply two Matrices
- Java Program to Reverse the Elements of Array
- Java Program to Reverse the First Half Elements of Array
- Java Program to Reverse the Second Half Elements of Array
- Java Program to find the Second Largest Number in the Array
- Java Program to find the Second Smallest Number in the Array
- Java Program to transpose the Matrix