# Programming Videos

## Search

### Java Program to Find Largest Number in an Array

Write a java program to find largest number in an array. Given an unsorted array, we have to write a java code to find largest number in an array.

### Algorithm to Find Largest Number in an Array

First Approach: Sort an array

First approach is to sort an array and pick the last element of an array. For sorting, we can use algorithm such as Bubble sortSelection sortInsertion sortQuick sort, Merge sort etc. to sort an array. After sorting, An array element at n-1 position is highest number of an array.

The time complexity of sorting an array is O(nlogn).

Bubble sort program in java

Sorting algorithms and their time complexity

Second Approach:

Traverse an array to find highest number of an array.

`1. Take a variable and assign the minimum value.`
```
```

2.  Traverse an array and compare each element of an array to the assigned variable.

Java program to find second smallest number in an array

## Java Program to Find Largest Number in an Array

In this program, we take the size of an array and array element as input from a user. Then we declare a variable highest and assign a minimum integer value. After that, we traverse an array and compare each element of array to the value of a variable.

```import java.util.Scanner;

public class Largest {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);
System.out.println("Enter the size of an array");

// Size of an array
int n = in.nextInt();

// Array declaration
int arr[] = new int[n];

System.out.println("Enter an array elements");

//Input array elements
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}

// Assign minimum value
int highest = Integer.MIN_VALUE;

for (int i = 0; i < n; i++) {
// compare
if (arr[i] > highest) {
highest = arr[i];
}
}

System.out.println("Highest number is " + highest);
}

}

```

Output :

Java programs for practice

Java program to print factorial of a number