Java Program to Find Largest Element in an Array

«
»
This is a Java Program to Find the Largest Number in an Array.

Enter the elements of array as input. By comparing elements of array with each other we get the largest number of the array.

Here is the source code of the Java Program to Find the Largest Number in an Array. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

advertisement
  1. import java.util.Scanner;
  2. public class Largest_Number 
  3. {
  4.     public static void main(String[] args) 
  5.     {
  6.         int n, max;
  7.         Scanner s = new Scanner(System.in);
  8.         System.out.print("Enter number of elements in the array:");
  9.         n = s.nextInt();
  10.         int a[] = new int[n];
  11.         System.out.println("Enter elements of array:");
  12.         for(int i = 0; i < n; i++)
  13.         {
  14.             a[i] = s.nextInt();
  15.         }
  16.         max = a[0];
  17.         for(int i = 0; i < n; i++)
  18.         {
  19.             if(max < a[i])
  20.             {
  21.                 max = a[i];
  22.             }
  23.         }
  24.         System.out.println("Maximum value:"+max);
  25.     }
  26. }

Output:

$ javac Largest_Number.java
$ java Largest_Number
 
Enter number of elements in the array:5
Enter elements of array:
4
2
3
6
1
Maximum value:6

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.