SECOND LARGEST VALUE IN AN ARRAY
PROGRAM TO FIND SECOND LARGEST VALUE IN AN ARRAY
import java.util.Scanner; //Import the Scanner class
class Second_largest
{
public static void main(String args[])
{
int num[] = new int[60];
int i, L=0, SL=0, size;
System.out.print("Enter array size: ");
Scanner in = new Scanner(System.in);
size=in.nextInt();
//Insert value in size variable at run time
//size=in.nextInt();
System.out.println("Enter values in array:");
//Scanner in = new Scanner(System.in);
for(i=0; i<size; i++)
{
num[i]=in.nextInt();
}
//Loop to find the largest value.
for(i=0; i<size; i++)
{
if(L < num[i])
L = num[i];
}
//Loop to find the second largest value.
for(i=0; i<size; i++)
{
if(num[i] < L)
{
if(SL < num[i])
SL = num[i];
}
}
System.out.println("Second largest value: "+SL);
}
}
0 Comments