Saturday 20 November 2021

Q6_smallest_array

smallest

6. Java Program to print the smallest element in an array

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

Original array

11,2,33,45,59

smallest element of array :-

2

Algorithm

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

Program

class Q6_smallest {

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 min=arr[0];

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

{

if(arr[i]< min)

{

min=arr[i];

}

}

//display smallest element of array

System.out.println("smallest elementof the given array is ="+min);

output

original array is:

11,2,33,45,59

smallest element of array is:

2

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...