• Register
1 vote
12 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.

12 7 7
15,250 points

1 Answer

1 vote
 
Best answer

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.

13 9 6
94,240 points

Related questions

1 vote
1 answer 12 views
12 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.
asked Mar 29 Gavin 15.3k points
0 votes
1 answer 15 views
15 views
I am new at java programing, I have an assginment about how to find largest number in array and I start coding on my own and i wrote a program given bellow : int size ; int array[] = {10, 20, 25, 63, 96, 57}; size = array.length; for(int i = 0; i<size; i++ ){ ... j]){ size = array[i]; } } System.out.println("Third largest element is:: "+array[size]) } but it is not running, what should I do now?
asked Mar 16 LizzyM 6.1k points
3 votes
1 answer 19 views
19 views
Problem: Hello good souls, I am a new student of Java. I just started learning and trying to solve java problems. Right now, I am working with the arrays. I can do some basic things with an array. I am finding something more advance problems of an array. Thus I thought to find the ... in java? Let&rsquo;s say I have an array Integer[] num = { 2, 4, 6, 5, 8 }; Thanks in advance for your effort.
asked Mar 23 Gavin 15.3k points
1 vote
1 answer 16 views
16 views
Problem: Supose there are 3 numbers. num1 = 114 num2 = 88 num3 = 279 I tried to find the largest number between them using python.So I wrote the code: num1 = float(input("Enter your first number: ")) num2 = float(input("Enter your second number: ")) num3 = ... num2 > num3): ^ SyntaxError: invalid syntax Can anyone help me out to solve this problem? And how can I take the numbers input from users?
asked Jun 11 samhaz 5k points
0 votes
1 answer 12 views
12 views
I want to know how to sort an array in java without using sort method.
asked Sep 28 Daniel Anderson 4k points