This is the first solution to find the first and second biggest numbers from the given array of integers, without using any built in methods.
import java.util.Scanner;
public class FirstAndSecondBigNums{
public static void main(String [] args) {
Scanner inpInt=new Scanner(System.in);
System.out.println("Enter number of integers input to the array");
int numInt=inpInt.nextInt();
int [] arrNums=new int[numInt];
int bigNum, secBignum;
//Input number of integers into an array
System.out.println("Enter "+numInt+" integers");
for(int i=0;i<numInt;i++) {
arrNums[i]=inpInt.nextInt();
}
//Assign the by default the arrNums[0] to the variables first big and second big
bigNum=secBignum=arrNums[0];
//Find the first big and second big numbers from this array of integers
for(int i=1;i<numInt;i++) {
if(arrNums[i]>bigNum) {
secBignum=bigNum;
bigNum=arrNums[i];
}
else if (arrNums[i]<bigNum && arrNums[i]>secBignum) {
secBignum=arrNums[i];
}
}
System.out.println("The biggest number in the array is="+bigNum);
System.out.println("The second biggest number in the array is="+secBignum);
}
}
Output:
Enter number of integers input to the array
8
Enter 8 integers
45
87
64
127
32
59
94
19
The biggest number in the array is=127
The second biggest number in the array is=94

Leave a comment