write a java program to store salary of 10 persons and display the salary in descending order using sorting method.
Answers
Answered by
4
import java.util.*;
public class Salary
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String[] names = new String[10];
int[] sal = new int[10];
for(int i=0;i<10;i++)
{
System.out.print("Enter Name of Person "+(i+1)+" : ");
names[i] = sc.next();
System.out.print("Enter Salary of "+names[i]+" : ");
sal[i] = sc.nextInt();
System.out.println();
}
int n = sal.length;
int temp = 0;
String ts = "";
for(int i=0; i < n; i++)
{
for(int j=1; j < (n-i); j++)
{
if(sal[j-1] > sal[j])
{
temp = sal[j-1];
sal[j-1] = sal[j];
sal[j] = temp;
ts = names[j-1];
names[j-1] = names[j];
names[j] = ts;
}
}
}
System.out.println("\n\nSalaries in Decreasing Order are: \n\n");
for(int i=9;i>=0;i--)
{
System.out.println(names[i]+" has a salary of "+sal[i]);
}
}
}
public class Salary
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String[] names = new String[10];
int[] sal = new int[10];
for(int i=0;i<10;i++)
{
System.out.print("Enter Name of Person "+(i+1)+" : ");
names[i] = sc.next();
System.out.print("Enter Salary of "+names[i]+" : ");
sal[i] = sc.nextInt();
System.out.println();
}
int n = sal.length;
int temp = 0;
String ts = "";
for(int i=0; i < n; i++)
{
for(int j=1; j < (n-i); j++)
{
if(sal[j-1] > sal[j])
{
temp = sal[j-1];
sal[j-1] = sal[j];
sal[j] = temp;
ts = names[j-1];
names[j-1] = names[j];
names[j] = ts;
}
}
}
System.out.println("\n\nSalaries in Decreasing Order are: \n\n");
for(int i=9;i>=0;i--)
{
System.out.println(names[i]+" has a salary of "+sal[i]);
}
}
}
QGP:
Hope it helps.
Similar questions
English,
7 months ago
Science,
7 months ago
Social Sciences,
1 year ago
Math,
1 year ago
Physics,
1 year ago