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