Find elements

Java code to find largest elements of an Array

Java code to find largest elements of an Array

In this tutorial, we will discuss a concept of Java code to find the largest elements of an Array

In this topic, we learn how to find largest element of an array(Collection of elements)

This program gets “n” number of elements and Enter the elements of the array as input from the user. then, this program finds and displays the largest elements from the array using for loops.

In this programs, we can see step by step procedure for completion of the program.

  • array declaration
  • Essential variable declaration
  • Get input from the user for array length
  • Get input from the user for array elements
  • loop for finding largest value
  • Display result on the screen

Find largest value – integer Array

import java.util.Scanner;
class Array_Large{
public static void main (String args[]){
int max;//variable declaration
System.out.println("Enter the number of elements in an array: ");
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();//get input from user for array length
int[] arr=new int[n];//declaring an array of n elements

//for loop for get input from user
for(int i=0; i<n; i++){
System.out.print("Enter the element "+(i+1)+" :");//n elements
arr[i]=scan.nextInt();//get input from user for array
}
max=arr[0];
for(int i=0; i<n; i++){
if(max<arr[i]){
max=arr[i];
        }
}
System.out.println("\nThe largest value is: "+max);
}//display result on the screen
}

When the above code is compiled and executed, it produces the following results

Enter the number of elements in an array:
5
Enter element 1: 67
Enter element 2: 98
Enter element 3: 64
Enter element 4: 90
Enter element 5: 23

The largest value is: 98

 

Find largest value – float Array

import java.util.Scanner;
class Large_num{
public static void main (String args[]){
Scanner scan=new Scanner(System.in);
System.out.print("Enter the number of elements in an array: ");
double max;
int n=scan.nextInt();//get input from user for array length
double arr[]=new double[n]; //declaring an array of n elements

//for loop takes input from user
for(int i=0; i<n; i++){
   System.out.print("Enter element "+(i+1)+": ");
   arr[i]=scan.nextDouble();//takes input from user for array
   }
   max=arr[0];
   for(int i=0; i<n; i++){
     if(max<arr[i]){
       max=arr[i];
     }
   }
   System.out.print("\nThe largest value is: "+max);
}//display result on the result
}

When the above code is compiled and executed, it produces the following results

Enter the number of elements in an array: 5
Enter element 1: 4.2
Enter element 2: 2.4
Enter element 1: 6.7
Enter element 2: 9.5
Enter element 2: 5.6

The largest elemets is: 9.5

Similar post

 largest number of three number in C

 largest number of three number in C++

 largest number of three number in Python

 largest number of three number in C using function

 largest number of three number in C++ using function

 largest number of three number in Python using function

 largest number of three number in Java using method

 

Suggested for you

Operator in Java language

For loop in Java language

If statements in Java language

Data type in Java language

Single dimension array in Java language

Two dimension array in Java language

Three dimension array in Java language

 

Operator in C language with example

For loop in C language 

If statements in C language

Data type in C language

Single dimension array in C language

Two dimension array in C language

Three dimension array in C language

 

Cpp program to find largest elements of an Array
Java code to find smallest number in an array
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

22 hours ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago