# How to find the largest number in an array java?

1 vote
30 views

## Problem:

Hello Kodlogs,

I have got a question from my fellow (java learner) friend and the question is: find the largest number in an array java

I know about the array and I can print all the values sequentially. Moreover, I can add two different values of an array in java, but I couldn't catch the logic of the problem given to me. It seems to me that, it might be something array sorting though I am not sure at all.

Could any of you please give me a proper solution and tell me how to find the largest number in an array java?

Thanks to all.

1 vote

## Solution:

This is a pretty much easy program to write. We will declare a variable (say maximum) and store the value of the first element of the given array. Furthermore, we will check every element of the array and compare with the variable maximum. By the end of the iteration, we will have our largest element inside our function largest().  We will call upon the function inside our main method and simply print it.

``````public class Test{
static int myarr[] = {10,24,15,9,98};
static int largest(){
int n;
int maximum = myarr[0];
for (n = 1; n < myarr.length; n++)
if (myarr[n] > maximum)
maximum = myarr[n];
return maximum;
}

public static void main(String[] args){
System.out.println("Largest number is: " + largest());
}
}``````

I hope you get it. Leave a kudos if you have got the solution.
Thanks.

## Related questions

181 views
Problem: Hi there! I have started learning java just recently and I found a problem in a programming exercise in which I have to find the second largest number in a given array using programming. I know how to find the largest number, but I am facing difficulty ... number. Can anybody here help me solve the problem? Also, provide the example program so that I can understand it better. Thanks!
17 views
Problem I'm trying to find the largest number in the array in recusin java, but i am not getting the appropriate results So can anyone help me
1 vote
116 views
Problem: I am trying to finding the largest number in an array c++ Every time I am trying my code is printing all the values of the array instead of the largest one. I might be doing something wrong inside my iteration. Do you have any idea to do it more easily and efficiently? I&rsquo;d love to hear from you guys. Please shar your ideas about this. Thanks.
125 views
Problem: Python program to find second largest number in a listGiven a list of numbers, the task is to write a Python program to find the second largest number in given list. Examples: Input: list1 = [10, 20, 4] Output: 10 Input: list2 = [70, 11, 20, 4, 100] Output: 70 Method 1: Sorting is an easier but less optimal method. Given below is an O(n) algorithm to do the same. Python3 filter_none
166 views
Problem: Hello Programmers! I am a novice C programmer striving hard to learn as much programming as possible in minimum time. I came across an assignment in which the programmer has to write a program that prints the second largest number in an integer array. I tried writing such a program and I ... +) { if(arr[i] > largest) { largest = arr[i]; } } printf("%d", largest); return 0; } Output: 44
11 views
Problem: Please assist me in resolving this issue: Find missing number in array in java
6 views
Problem: Please assist me in resolving this issue: find second largest number in array in java
1 vote