import java.util.*;
class big
{
public static void main(String args[])
{
int i;
System.out.println("Enter the size of the list (It should be greater than five) ");
Scanner sc=new Scanner(System.in);
int n=Integer.parseInt(sc.nextLine());
int a[]=new int[n];
if(n>5)
{
System.out.println("Enter the elements ");
for(i=0;i<n;i++)
{
a[i]=Integer.parseInt(sc.nextLine());
}
System.out.println("Entered elements are ");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
//sorting the elements
for(i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.println("Sorted list");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
System.out.println("First 5 largest elements are ");
for(i=n-1;i>=n-5;i--)
{
System.out.println(a[i]);
}
}
else
{
System.out.println("INVALID entry!!!!!");
System.out.println("Enter the size of the list greater than 5!!! ");
}
}
}
output
D:\java\notepad>javac big.java
D:\java\notepad>java big
Enter the size of the list (It should be greater than five)
1
INVALID entry!!!!!
Enter the size of the list greater than 5!!!
D:\java\notepad>javac big.java
D:\java\notepad>java big
Enter the size of the list (It should be greater than five)
6
Enter the elements
11
23
42
7
3
90
Entered elements are
11
23
42
7
3
90
Sorted list
3
7
11
23
42
90
First 5 largest elements are
90
42
23
11
7
D:\java\notepad>
No comments:
Post a Comment