Page 383 - CA_Blue( J )_Class10
P. 383
To input name and marks of 15 students in 2 single-dimensional arrays. And print the name and
Program 4
marks of the students rank-wise. (Use Bubble sort technique)
1 import java.util.*;
2 class bubbsort
3 { public static void main()
4 { Scanner sc= new Scanner(System.in);
5 int i, j, tempmarks;
6 String tempname;
7 String name[] = new String[15];
8 int marks[] = new int[15];
9 for(i = 0;i<15;i++)
10 { System.out.print ("Enter Name of student " + (i+1) +" : ");
11 name[i] = sc.next() ;
12 System.out.print ("Enter Marks : ");
13 marks[i] = sc.nextInt();
14 }
15 for(i=0;i<14;i++)
16 { for(j = i+1;j<15;j++)
17 { if(marks[i]<marks[j]){
18 tempmarks = marks[i];
19 marks[i] = marks[j];
20 marks[j] = tempmarks;
21 tempname = name[i];
22 name[i] = name[j];
23 name[j] = tempname;
24 }
25 }}
26 System.out.println("Name and Marks of Students Rank-wise");
27 System.out.println ("Name \t\t Marks");
28 for(i = 0;i<15;i++)
29 System.out.println (name[i]+"\t\t"+marks[i]);
30 } }
You will get the following output:
Enter Name of student 1 : Aarav
Enter Marks : 79
Enter Name of student 2 : Vihaan
Enter Marks : 67
381
Arrays 381

