Question 64 : Write a program in Java to find the fifth highest number among a set of numbers taken as input from the user.
Java Program :
import java.io.*;
class high5
{
static int[] a ;
static void check()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i,k,c,s;
System.out.print("\fEnter number of inputs : ");
c=Integer.parseInt(br.readLine());
if(c<5)
check();
a=new int[c];
for(i=0;i<c;i++)
{
System.out.print("\nEnter a number : ");
a[i]=Integer.parseInt(br.readLine());
}
for(i=0;i<c-2;i++)
{
for(k=0;k<c-2;k++)
{
if(a[k+1]>a[k])
{
s=a[k];
a[k]=a[k+1];
a[k+1]=s;
}
}
}
display();
}
static void display()
{
System.out.print("\n5th highest element is : "+a[4]);
}
}
Java Program :
import java.io.*;
class high5
{
static int[] a ;
static void check()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i,k,c,s;
System.out.print("\fEnter number of inputs : ");
c=Integer.parseInt(br.readLine());
if(c<5)
check();
a=new int[c];
for(i=0;i<c;i++)
{
System.out.print("\nEnter a number : ");
a[i]=Integer.parseInt(br.readLine());
}
for(i=0;i<c-2;i++)
{
for(k=0;k<c-2;k++)
{
if(a[k+1]>a[k])
{
s=a[k];
a[k]=a[k+1];
a[k+1]=s;
}
}
}
display();
}
static void display()
{
System.out.print("\n5th highest element is : "+a[4]);
}
}
No comments:
Post a Comment