Home Java Programming Language / Programs / Question 5: Write a program to input name and percentage of 35 students of class X in two separate one dimensional arrays. Arrange students details according to their percentage in the descending order using selection sort method. Display name and percentage of first ten toppers of the class.
🚀 Programming Example

Question 5: Write a program to input name and percentage of 35 students of class X in two separate one dimensional arrays. Arrange students details according to their percentage in the descending order using selection sort method. Display name and percentage of first ten toppers of the class.

👁 180 Views
💻 Practical Program
📘 Step Learning
Learn this program step-by-step with algorithm, source code, output and detailed explanation.

📌 Information & Algorithm

Given Input:


Expected Output:


💻 Program Code

import java.util.Scanner;

public class RAnsariStudentPercentage
{
     public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        
        //Initialize the 2 SDA
        String names[] = new String[35];
        double percentage[] = new double[35];
        
        /*
         * Accept student details from user and store
         * in corresponding SDA
         */
        for (int i = 0; i < names.length; i++) {
            System.out.print("Enter name of student " 
                                    + (i + 1) + ": ");
            names[i] = in.nextLine();
            System.out.print("Enter percentage of student " 
                                    + (i + 1) + ": ");
            percentage[i] = in.nextDouble();
            in.nextLine();
        }
        
        //Selection Sort in Descending Order
        for (int i = 0; i < percentage.length - 1; i++) {
            int maxIdx = i;
            for (int j = i + 1; j < percentage.length; j++) {
                if (percentage[j] > percentage[maxIdx])
                    maxIdx = j;
            }
            
            double t = percentage[i];
            percentage[i] = percentage[maxIdx];
            percentage[maxIdx] = t;
            
            String name = names[i];
            names[i] = names[maxIdx];
            names[maxIdx] = name;
        }
        
        //Display first ten toppers
        System.out.println("Name\tPercentage");
        for (int  i = 0; i < 10; i++) {
            System.out.println(names[i] + '\t' + percentage[i]);
        }
    }
}
                        
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.