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

Arranging according to potential.

PROGRAM: Requested by Sir  Ankur Acharya   ARRANGING WORDS IN A STRING ACCORDING TO THEIR POTENTIAL The encryption of alphabets are to be done as follows: A = 1 B = 2 C = 3 . . . Z = 26 The potential of a word is found by adding the encrypted value of the alphabets. Example: KITE Potential = 11 + 9 + 20 + 5 = 45 Accept a sentence which is terminated by either “ . ” , “ ? ” or “ ! ”. Each word of sentence is separated by single space. Decode the words according to their potential and arrange them in ascending order. Output the result in format given below: Example 1 INPUT : THE SKY IS THE LIMIT. POTENTIAL : THE = 33 SKY = 55 IS = 28 THE = 33 LIMIT = 63 OUTPUT : IS THE THE SKY LIMIT Example 2 INPUT : LOOK BEFORE YOU LEAP. POTENTIAL : LOOK = 53 BEFORE = 51 YOU = 61 LEAP = 34 OUTPUT : LEAP BEFORE LOOK YOU SYNTAX import java.io.*; class Potential {     public static void main(String args[])throws IOException     {       ...

Disarium Number

AJ Java Programs Program 4: Disarium Number A number will be called DISARIUM if sum of its digits powered with their respective position is equal to the original number. For example  135 is a DISARIUM (Workings 1 1 +3 2 +5 3  = 135, some other  DISARIUM  are 89, 175, 518 etc) Syntax: import java.io.*; class disarium {     public static void main(String args[])throws IOException     {         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));         int n,n1,n2,c=0,sum=0,d=0;         System.out.println("Enter a Num");         n=Integer.parseInt(br.readLine());         n2=n1=n;         while(n2!=0)         {             n2=n2/10;             c++;         }       ...

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; ...