Java Program to Find Largest Number in an Array

This article is created to cover multiple programs in Java that find the largest number in an array, entered by user at run-time of the program.

Java Find Largest Number in Array using for Loop

The question is, write a Java program to find and print the largest number in an array of 10 numbers. The program given below is its answer:

import java.util.Scanner;

public class CodesCracker
{
   public static void main(String[] args)
   {
      int i, large;
      int[] arr = new int[10];
      Scanner scan = new Scanner(System.in);
      
      System.out.print("Enter 10 Numbers: ");
      for(i=0; i<10; i++)
         arr[i] = scan.nextInt();
      
      large = arr[0];
      for(i=1; i<10; i++)
      {
         if(large<arr[i])
            large = arr[i];
      }
      
      System.out.println("\nLargest Number = " +large);
   }
}

The snapshot given below shows the sample run of above program, with user input 50, 51, 52, 53, 59, 58, 54, 55, 56, 57 as ten numbers for the array to find and print the largest one, among all these ten numbers:

java find largest number in array

The above program only works with 10 numbers. Therefore let's modify the program and create another, that find and prints largest number in an array of n numbers:

import java.util.Scanner;

public class CodesCracker
{
   public static void main(String[] args)
   {
      int n, i, large;
      Scanner scan = new Scanner(System.in);
      
      System.out.print("Enter the Value of n: ");
      n = scan.nextInt();
      int[] arr = new int[n];
      System.out.print("Enter " +n+ " Numbers: ");
      for(i=0; i<n; i++)
         arr[i] = scan.nextInt();
      
      large = arr[0];
      for(i=1; i<n; i++)
      {
         if(large<arr[i])
            large = arr[i];
      }
      
      System.out.println("\nLargest Number = " +large);
   }
}

The sample run with user input 6 as size or value of n and 1, 2, 3, 4, 5, 6 as six elements or numbers is shown in the snapshot given below:

find largest number in array java

Java Find Largest Number in Array using while Loop

Here is the last program of this article, create using while loop, instead of for.

import java.util.Scanner;

public class CodesCracker
{
   public static void main(String[] args)
   {
      Scanner scan = new Scanner(System.in);
      
      System.out.print("Enter the Value of n: ");
      int n = scan.nextInt();
      int[] arr = new int[n];
      System.out.print("Enter " +n+ " Numbers: ");
      int i=0;
      while(i<n)
      {
         arr[i] = scan.nextInt();
         i++;
      }
      
      int large = arr[0];
      i = 1;
      while(i<n)
      {
         if(large<arr[i])
            large = arr[i];
         i++;
      }
      
      System.out.println("\nLargest Number = " +large);
   }
}

This program produces same output as of previous program.

Same Program in Other Languages

Java Online Test


« Previous Program Next Program »


Liked this post? Share it!