Saturday, 20 November 2021

Q5_largest_array

Largest

5. Java Program to print the largest element in an array

In this program, we need to compaire each elemrnt of the given array to each other and find out the largest element in the present array . This can be accomplished by looping through the array and store in greater the elements of the array into max variable array at the corresponding position.

Original array

11,2,33,45,59

Largest element of array :-

59

Algorithm

  • 1.START
  • 2.INITIALIZE arr1[] ={11,2,33,45,59}
  • 3.i=a.lenght;i>0,i--
  • 4.(arr[i]>max)
  • 5.max=arr[i]; store in max varible
  • 4.display max element of array

Program

class Q5_largest {

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] + " ");

}

int max=arr[0];

for(int i=0;i< arr.length;i++)

{

if(arr[i]>max)

{

max=arr[i];

}

}

//display largest element of array

System.out.println("Largest elementof the given array is ="+max);

output

original array is:

11,2,33,45,59

largest element of array is:

59

Next Topic

No comments:

Post a Comment

How do you select data from a table in SQL?

Create Table How do you select data a table in SQL? The SELECT statement is used to se...