Computer Applications
Write a program to input and store roll number and total marks of 20 students. Using Bubble Sort technique generate a merit list. Print the merit list in two columns containing roll number and total marks in the below format:
Roll No. Total Marks
… …
… …
… …
Answer
import java.util.Scanner;
public class MeritList
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n = 20;
int rollNum[] = new int[n];
int marks[] = new int[n];
for (int i = 0; i < n; i++) {
System.out.print("Enter Roll Number: ");
rollNum[i] = in.nextInt();
System.out.print("Enter marks: ");
marks[i] = in.nextInt();
}
//Bubble Sort
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (marks[j] < marks[j + 1]) {
int t = marks[j];
marks[j] = marks[j+1];
marks[j+1] = t;
t = rollNum[j];
rollNum[j] = rollNum[j+1];
rollNum[j+1] = t;
}
}
}
System.out.println("Roll No.\t\tTotal Marks");
for (int i = 0; i < n; i++) {
System.out.println(rollNum[i] + "\t\t" + marks[i]);
}
}
}Output
Related Questions
Consider the following program segment and answer the questions given below:
int x[][] = {{2,4,5,6}, {5,7,8,1}, {34, 1, 10, 9}};(a) What is the position of 34?
(b) What is the result of x[2][3] + x[1][2]?
What is the output of the Java code given below?
String color[] = {"Blue", "Red", "Violet"}; System.out.println(color[2].length());- 6
- 5
- 3
- 2
Define a class to search for a value input by the user from the list of values given below. If it is found display the message "Search successful", otherwise display the message "Search element not found" using Binary search technique.
5.6, 11.5, 20.8, 35.4, 43.1, 52.4, 66.6, 78.9, 80.0, 95.5.