Hello code hungers, this is the java program to return the second last N number out of given N numbers.
import java.util.Scanner;
public class Secondmax
{
public static void main(String[] args)
{
int n, temp;
Scanner s = new Scanner(System.in);
System.out.println("Enter no. of elements you want in array(Minimum 2)");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all "+n+" elements");
for (int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.println("Second Largest Is:"+a[n-2]);
}
}
Output:
I hope you like the program and I request you to share my blog with your friends, also check out my youtube channel: https://www.youtube.com/channel/UC9C3DXQrgE6TI9YvMDQocrQ
Comments
Post a Comment