Skip to main content

Second largest element

AJ Java Programs

Program 5:

Finding Second Largest Element in Array
Ex: {2,3,4,5,3,1} are elements then 4 is second largest element

Syntax:

Way 1: PreDefined

import java.io.*;
public class FindsecondLargest
{
    public static void main(String[] args) {
        // intialize the array values
        int arr[] = { 1, 23, 47, 81, 92, 52, 48, 56, 66, 65 };
        int largest = arr[0];
        int secondLargest = arr[0];
        // check the condition
        for (int i = 0; i < arr.length; i++) {
            // this condition check for largest number
            if (arr[i] > largest) {
                secondLargest = largest;
                largest = arr[i];

            } else if (arr[i] > secondLargest) {
                secondLargest = arr[i];

            }
        }
        // print the result
        System.out.println("second largest number is:" + secondLargest);

    }
}

Way 2: UserDefined

import java.io.*;
public class secondLargest
{
    public static void main(String[] args)throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       
        // intialize the array values
        System.out.println("Enter number of elements you want to insert in array");
        int n=Integer.parseInt(br.readLine());
       
        int arr[] = new int[n+1];
        System.out.println("Enter elements");
        for(int j = 0;j < n; j++)
        {
            arr[j] = Integer.parseInt(br.readLine());
           
        }
        int largest = arr[0];
        int secondLargest = arr[0];
        // check the condition
        for (int i = 0; i < arr.length; i++) {
            // this condition check for largest number
            if (arr[i] > largest) {
                secondLargest = largest;
                largest = arr[i];

            } else if (arr[i] > secondLargest) {
                secondLargest = arr[i];

            }
        }
        // print the result
        System.out.println("second largest number is:" + secondLargest);

    }
}

Tested Ok Program
If You Don't Understand anything or want any program please write your request in Comment Section
And Please Like The Post

Comments

Popular posts from this blog

SMITH NUMBER

PROGRAM: SMITH NUMBER A   Smith number  is a composite number, the sum of whose digits is the sum of the digits of its prime factors obtained as a result of prime factorization (excluding 1). The first few such numbers are 4, 22, 58, 85, 94, 121 ……………….. For Example 58=> 5+8=13 2+(2+9) of 29 =13 Since sum of digits=sum of digits of prime factors 58 is a SMITH NUMBER SYNTAX: import java.io.*; class smith_no { static int sum,sum1;     public static void main(String args[])throws IOException     {         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));         int n;int c=0;         System.out.println("Enter a Number");         n=Integer.parseInt(br.readLine());         int n1=n;         while(n1!=0){         int d=n1%10;         sum1+=d; ...

LUCKY NUMBER

PROGRAM : LUCKY NUMBER QUESTION: Consider the sequence of natural numbers. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 …………………………………. Removing every second number produces the sequences 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23 …………………………. Removing every third number produces the sequences 1, 3, 7, 9, 13, 15, 19, 21, 25 …………………………. This process continues indefinitely by removing the fourth, fifth…and so on, till after a fixed number of steps, certain natural numbers remain indefinitely. These are known as  Lucky Numbers . Write a program to generate and print lucky numbers less than a given number N. SAMPLE INPUT :   N = 10 OUTPUT :   The Lucky numbers less than 10 are:     1     3     7 SAMPLE INPUT :   N = 25 OUTPUT :   The Lucky numbers less than 25 are:     1     3  ...

MAGICAL/ MAGIC NUMBER USING RECURSION

PROGRAM :  MAGICAL NUMBER BY RECURSION This  program  checks if a  number  is a  Magic number  in  JAVA . A  number  is said to be a  Magic number  if the sum of its digits are calculated till a single digit is obtained by recursively adding the sum of its digits. If the single digit comes to be 1 then the  number  is a  magic number . Example: 10999 => 1+0+9+9+9=28=>2+8=>10=>1+0= 1                  199    =>1+9+9 =>19=> 1+9=>10=>1+0= 1 SYNTAX import java.io.*; class magic  {    static int c;     public static void main(String arsg[])throws IOException     {         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));         int n;int d=0;int m;         System.out.println("ENter a NUmber");   ...