# Thread: Finding largest no

1. Senior Member Join Date
Nov 2007
Posts
111
Rep Power
0

## Finding largest no

I have following code for finding largest no among 3 nos.
Java Code:
```import java.util.*;
public class LargestNo{
public static void main (String [] arg) {

Scanner scan = new Scanner (System.in);

int [] numbers = new int ;
int x;
int largestNumber;

System.out.print("Put in (three) numbers");
for (x=0; x<numbers.length; x++) {
numbers[x]=scan.nextInt ();
}

largestNumber = 0;
for (x=0; x<numbers.length; x++) {
if (x == 0) {
largestNumber = numbers;
}
if (numbers[x] > largestNumber) {
largestNumber = numbers[x];
}
}
System.out.println("The largest number is " + largestNumber);

}
}
}```
I want to print the largest no and also its position in the array. how to do that? Please give me tips.

Cheers  Reply With Quote

2. Member Join Date
Aug 2007
Posts
26
Rep Power
0

## You can have a separate int variable for position.
something like this...
Java Code:
```import java.util.*;
public class LargestNo{
public static void main (String [] arg) {

Scanner scan = new Scanner (System.in);

int [] numbers = new int ;
int x;
int largestNumber;
int pos=0;//position in the array

System.out.print("Put in (three) numbers");
for (x=0; x<numbers.length; x++) {
numbers[x]=scan.nextInt ();
}

largestNumber = 0;

for (x=0; x<numbers.length; x++) {
if (x == 0) {
largestNumber = numbers;
pos = 0;
}
if (numbers[x] > largestNumber) {
largestNumber = numbers[x];
pos = x;//x is the position of the number in the array.
}
}
System.out.println("The largest number is " + largestNumber);
System.out.println("Position of the largest number:"+pos);

}
}```  Reply With Quote

3. Senior Member Join Date
Nov 2007
Posts
111
Rep Power
0

## Ok kool. Cant I fetch the index from array just specifying the value. For example, I am assuming that array will only contain unique values and having a value I now want to get its index from array.  Reply With Quote

4. ## Just like same thing you have to done. Use another dummy variable. At each of the comparison you have done to check whether the number is large or small, update that dummy value.

Say first number is large, your dummy should be 0. In the next iteration the second number is small, still your dummy should be 0, because it holds the position.

I think my logic is clear.

At the same time, I think you have use additional } at last. Compile and check it.  Reply With Quote

5. Senior Member Join Date
Nov 2007
Posts
111
Rep Power
0

## Thanks Eranga. But consider the following:
For example, I am assuming that array will only contain unique values and having a value I now want to get its index from array.  Reply With Quote

6. ## I'm not get you. What you mean unique values. Between array index and array values there is no connection. Can you explain little more.  Reply With Quote

7. Senior Member Join Date
Nov 2007
Posts
111
Rep Power
0

##  Originally Posted by Eranga Between array index and array values there is no connection.
This clears the confusion. I was assuming that there is a connection and we can retrieve the values both way some how.  Reply With Quote

8. ## No there is no connection. I think you know that, array is indexing by starting with 0. That mean maximum indexing is less than one by number of element.

element 12 5 23
index 0 1 2
#of element 1 2 3  Reply With Quote

9. Member Join Date
Aug 2007
Posts
26
Rep Power
0

## There is actually a way to get the index..Arrays has a method
binarySearch(array to be searched,key) which returns the position of the specified key..
Using it in your program:

Java Code:
```import java.util.*;
public class LargestNo{
public static void main (String [] arg) {

Scanner scan = new Scanner (System.in);

int [] numbers = new int ;

int x;
int largestNumber;
//int pos=0;//position in the array
int index=0;

System.out.print("Put in (three) numbers");
for (x=0; x<numbers.length; x++) {
numbers[x]=scan.nextInt ();
}

largestNumber = 0;

for (x=0; x<numbers.length; x++) {
if (x == 0) {
largestNumber = numbers;
//pos = 0;
}
if (numbers[x] > largestNumber) {
largestNumber = numbers[x];
//pos = x;//x is the position of the number in the array.
index = Arrays.binarySearch(numbers, largestNumber);
}
}
System.out.println("The largest number is " + largestNumber);
System.out.println("Position of the largest number:"+index);

}
}```
I am not sure though if this would be a correct way to get the index of an array element...

Thanks,
R  Reply With Quote

10. ## Yep that's correct. One of the efficient way it is.

The way what I've told is the basis way to do it, comparing how to find the largest number. Because the same way should follow.  Reply With Quote

11. Senior Member Join Date
Nov 2007
Posts
111
Rep Power
0

## Its clear now. Thanks all of you.  Reply With Quote

12. ## You are welcome pal....  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•