Monday, 15 January 2018

Find Largest two number in array in java

package com.tutorial4u;

import java.util.Scanner;

public class LargestTwoNumber {
       public static void main(String[] args) {
             int num ,temp;
             System.out.print("Enter element what you want in array : ");
             Scanner sc  = new Scanner(System.in);
             num = sc.nextInt();
             int a[] = new int[num];
             System.out.println("Enter all the element : ");
             for(int i=0;i<num;i++){
                    a[i]=sc.nextInt();
             }
             for(int i=0;i<num;i++){
                    for(int j=i+1 ;j<num;j++){
                           if(a[i]<a[j]){
                                 temp = a[i];
                                 a[i] = a[j];
                                 a[j] = temp;
                           }
                    }
             }
             System.out.println("Largest two number in array is "+a[0]+","+a[1]);
            
       }


}

Output:


Enter element what you want in array : 4
Enter all the element :
12
23
54
10
Largest two number in array is 54,23

No comments:

Post a Comment

No of Occurrence in Array

package com.tutorial4u; import java.util.HashMap; /************************************************************  No of Occurrence in Array...