7. Java Program to print the sum of all the items of the array in Java
In this program, we need to compaire each elemrnt of the given array to each element adding each other and thorugh out the addition of the given array is . This can be accomplished by looping through the array and store the elements of the array at the corresponding position.
Original array
1,2,3,4,5
additionof element array :-
15
Algorithm
- 1.START
- 2.INITIALIZE arr1[] ={1,2,3,4,5}
- 3.i=a.lenght;i>0,i--
- 4.sum=sum=arr[i]
- 5.sum store in addition varible
- 4.display max element of array
Program
class Q7_sum {
public static void main(String[] args)
//declare array
int arr[]={11,2,33,45,59};
System.out.println("original array is");
for(int i=0;i< arr.length;i++)
{
System.out.print(arr[i] + " ");
}
for(int i=0;i< arr.length;i++)
{
sum=sum+arr[i];
}
//display largest element of array
System.out.println("addition of element of the given array is ="+sum);
output
original array is:
1,2,3,4,5
addition element of array is:
15
Next Topic
No comments:
Post a Comment