Computer Application Practical File
Computer Application Practical File
NO VARIABLES USED
1
PROGRAM 2
public class Operators
{
public static void main(String args[])
{
int a = 30;
int b = 24;
int c = a - b;
name a b c
2
PROGRAM 3
public class p3
{
public static void main(String args[])
{
double c = 135.0;
double r = 135.0 / (2.0 * Math.PI);
System.out.println("The radius is: " + r);
name c r a
3
PROGRAM 4
public class Rectangle
{
public static void main(String args[])
{
int l = 4;
int b = 8;
int Area = l * b;
int Perimeter = 2 * (l + b);
4
PROGRAM 5
public class p5
{
public static void main(String args[])
{
double s = 15.0;
double a = s * s;
double p = 4.0 * s;
double d = Math.sqrt(s * s + s * s);
name s a p d
5
PROGRAM 6
public class Weights
{
public static void main(String args[])
{
double kg = 4.8;
double g = kg * 1000;
6
PROGRAM 7
public class ASCII
{
public static void main(String args[])
{
char ch = 'Z';
int a = ch;
char cb = 'z';
int b = cb;
int c = b + a;
int d = b - a;
7
PROGRAM 8
public class eight {
public static void main(int seconds) {
int hours = seconds / 3600;
int rs = seconds % 3600;
int minutes = rs / 60;
int s = rs % 60;
8
PROGRAM 9
public class temperature
{
public static void main(String args[])
{
double f = 98.6; // Example value
double c = (f - 32.0) * 5.0 / 9.0;
9
PROGRAM 10
public class P10
{
public static void main(String args[])
{
double sp = 120.0; // Example selling price
10
PROGRAM 11(With using a third variable)
public class Practical_11 {
public static void main(int a, int b) {
int c = a;
a = b;
b = a;
11
PROGRAM 11(Without using a third variable)
INPUT
A = 12
B =17
OUTPUT
12
PROGRAM 12
import java.util.*;
public class Practical12
{
public static void main()
{
Scanner sc = new Scanner(System.in);
double a;
System.out.println("Enter your Marks");
a = sc.nextDouble();
if (a >= 90)
{
System.out.println("Your Grade is A");
}
else if (a >= 70)
{
System.out.println("B");
}
else if (a >= 50)
{
System.out.println("C");
}
else
{
System.out.println("D");
}
}
}
OUTPUT
VARIABLE DESCRIPTION
Name of Variable Data type Purpose
13
PROGRAM 13
public class Practical13 {
public static void main(double a) {
double b, c, d, e, f, g;
b = Math.abs(a);
c = Math.sqrt(a);
d = Math.cbrt(a);
e = Math.round(a);
f = Math.ceil(a);
g = Math.floor(a);
14
PROGRAM 14
public class Practical14 {
public static void main(double a, double b, double c) {
System.out.println("The entered numbers are :" + a + b + c);
PROGRAM 15
15
import java.util.*;
public class p15
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a, b, c;
System.out.println("Enter the First no:");
a = sc.nextInt();
System.out.println("Enter the Second no:");
b = sc.nextInt();
System.out.println("Enter the Third no:");
c = sc.nextInt();
PROGRAM 16
16
import java.util.*;
public class p16
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
double a;
double b = 200.0;
System.out.println("Enter amount in units");
a = sc.nextDouble();
System.out.println("Total cost=");
if (a <= 99)
System.out.println((2 * a) + b);
else if (a >= 100 && a <= 199)
System.out.println((1.80 * a) + b);
else if (a >= 200)
System.out.println((1.50 * a) + b);
}
}
a double
b double
PROGRAM 17
17
public class p17
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
double a;
System.out.println("Enter the Number:");
a = sc.nextDouble();
if (a % 5 == 0 && a % 3 == 0)
System.out.println("The number is divisible both by 5 & 3");
else if (a % 5 != 0 && a % 3 == 0)
System.out.println("The number is divisible only by 3");
else if (a % 3 != 0 && a % 5 == 0)
System.out.println("The number is divisible only by 5");
else if (a % 3 != 0 && a % 5 != 0)
System.out.println("The number is not divisible both by 5 & 3");
}
}
18
PROGRAM 18
import java.util.*;
19
System.out.println("Gift=Wall clock");
System.out.println("Discounted price=" + dis15);
} else {
System.out.println("Discount=20%");
System.out.println("Gift=Wrist watch");
System.out.println("Discounted price=" + dis20);
}
}
}
20
PROGRAM 19
Code:
public class P19
{
public static void main(String args[])
{
int i;
int a=21;
for(i=1;i<=10;i++)
System.out.println("21x"+1+"=" + a * i);
}
}
21
PROGRAM 20
import java.util.*;
public class p20 {
public static void main(String args[]) {
Scanner last = new Scanner(System.in);
System.out.print("Enter number");
int n = last.nextInt();
int f = 1;
PROGRAM 21
import java.util.*;
22
public class p21 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int number = sc.nextInt();
int same = Math.abs(number); // store absolute number
int sum = 0;
int count = 0;
if (number == 0) {
sum = 0;
count = 1;
}
23
purpose To store the To accept and To store int To store the To perform
absolute store users value after int value calculations
value of users value calculation during while the
input looping(contr loop runs
ol variable)
PROGRAM 22
import java.util.*;
public class p22 {
public static void main(String args[]) {
int i, f1 = 1, f2 = 1, f3 = 1, f4 = 1, f5 = 1;
24
for (i = 4; i >= 1; i--) {
f2 *= i;
}
int a = f1 + f2 + f3 + f4 + f5;
System.out.println("The sum: " + a);
}
}
25
while while while while while while calculatio
being being being being being being n
incremen incremen incremen incremen incremen incremen
ted(contr ted and ted and ted and ted and ted and
ol multiplie multiplie multiplie multiplie multiplie
variable) d d d d d
PROGRAM 23
import java.util.*;
public class p23 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int p, q, i, a;
System.out.println("The Buzz numbers between " + p + " and " + q + " = ");
for (i = p; i <= q; i++) {
a = i - 7;
if (a % 10 == 0 || i % 7 == 0) {
System.out.println(i);
}
}
}
}
26
VARIABLE DESCRIPTION TABLE:
name p q i a
PROGRAM 24 a
import java.util.*;
public class p24_1
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Range");
int a = sc.nextInt();
int sum = 5;
for (int i = 5; i <= a; i++)
{
if (i % 2 != 0)
27
sum += i;
}
System.out.println(sum);
}
}
28
PROGRAM 24 b
import java.util.*;
29
PROGRAM 24 c
import java.util.*;
public class p24_3 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the range");
double r = sc.nextDouble();
System.out.println("Enter a number");
double a = sc.nextDouble();
double s = 0;
System.out.println(s);
}
}
name r a s i b
30
PROGRAM 25 a
import java.util.*;
System.out.println("Enter a number");
a = sc.nextInt();
System.out.println(s);
}
}
31
PROGRAM 25 b
public class p25_2 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of digits to display");
int a = sc.nextInt();
int b = 0;
32
PROGRAM 26
import java.util.*;
int b = Math.abs(a);
int sum = 0;
while (b > 0) {
int last = b % 10; // Get the last digit
sum += last; // Add it to sum
b /= 10; // Remove the last digit
}
33
PROGRAM 27 a
public class p27_a {
public static void main(String args[]) {
int i;
for (i = 5; i >= 1; i--) {
for (int a = 1; a <= i; a++) {
System.out.print("#");
}
System.out.println();
}
}
}
name i a
34
PROGRAM 27 b
import java.util.*;
public class p27_2
{
public static void main(String[] args) {
int n = 4;
35
PROGRAM 28(a)
public class twentyeighta
{
public static void main(String args[])
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
36
PROGRAM 28(b)
public class twentyeight{
public static void main(String args[] {
int a = 15;
for (int i = 5; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
System.out.print(a + " ");
a--;
}
System.out.println();
}
}
}
37
PROGRAM 28(c)
public class twentyeightc
{
public static void main(String args[])
{
int a = 1;
for (int i = 5; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
System.out.print(a + " ");
}
System.out.println();
a += 2;
}
}
}
38
PROGRAM 28(d)
public class twentyeightd
{
public static void main(String args[])
{
for (int i = 9; i >= 1; i -= 2)
{
for (int j = i; j <= 9; j += 2)
{
System.out.print(j + " ");
}
System.out.println();
}
}
}
39
PROGRAM 29
import java.util.*;
public class twentynine {
public static void main(String args[]) {
Scanner a = new Scanner(System.in);
System.out.println("ENTER A NUMBER");
int i = a.nextInt();
int j = i;
for (int c = 0; c <= 99; c++) {
int sum = 0;
for (int q = i; q > 0; q /= 10) {
int e = q % 10;
sum += e * e;
}
i = sum;
if (i == 1) {
System.out.println(j + " IS A HAPPY NUMBER");
break; }
}
if (i != 1) {
System.out.println(j + " ISN’T A HAPPY NUMBER");
}
}
}
40
VARIABLE DESCRIPTION TABLE:
Name of Variable Data type Purpose
i Int To take input as an integer
j Int To assign value and declare
as an integer
c Int To assign value and declare
as an integer
sum Int To assign value and declare
as an integer
q Int To assign value and declare
as an integer
e Int To assign value and declare
as an integer
`
41
PROGRAM 30
import java.util.*;
if (a % j == 0) {
System.out.println("Composite Number");
} else if (a % j != 0) {
System.out.println("Not a Composite Number");
}
}
}
42
VARIABLE DESCRIPTION TABLE:
Name of Variable Data type Purpose
43
PROGRAM 31
CODE:
import java.util.*;
class p31
{
public static void main (String args[])
{
Scanner sc = new Scanner(System.in);
int n,l,s=0,a=10;
System.out.println("Enter a number");
int b=sc.nextInt();
n=b;
do
{
l=n%10;
s*=10;
s+=l;
n/=10;
}
while(n>0);
System.out.println("the reversed digit: " + s);
System.out.println("Absolute difference: " + (b-s));
}
}
44
VARIABLE DESCRIPTION TABLE:
name Data type purpose
45
PROGRAM 32
CODE:
import java.util.*;
class p32
{
Scanner sc= new Scanner(System.in);
int vno,hours;
double bill;
void input()
{
System.out.println("Enter the vehicle number: ");
vno=sc.nextInt();
System.out.println("Enter the number of hours: ");
hours=sc.nextInt();
}
void calculate()
{
if (hours==1)
{
bill=3.0;
}
else if(hours>1)
{
double d1=(hours-1)*1.50;
bill=3.0 + d1;
}
46
}
void display()
{
System.out.println(" the vehicle number : " + vno);
System.out.println(" the number of hours : " +hours);
System.out.println(" the amount : " + "rupees "+bill);
}
public static void main(String args[])
{
p32 obj=new p32();
obj.input();
obj.calculate();
obj.display();
}
47
VARIABLE DESCRIPTION TABLE:
name Data type purpose
48
PROGRAM 33
CODE:
import java.util.*;
class p33
{
Scanner sc = new Scanner (System.in);
int taxi_no,km,bill;
String name;
void input()
{
System.out.println("Enter The Name");
name=sc.nextLine();
System.out.println("Enter number of kilometres travelled");
km=sc.nextInt();
}
void calculate()
{
if(km==2)
{ bill = 25;
}
else if(km>2&&km<=10)
{ bill = 10*km;
}
else if(km>10&&km<=20)
{ bill = 15 *km;
49
}
else if(km>20)
{ bill = 20 *km;
}
}
void main()
{
System.out.println("The Name is : " + name);
System.out.println("The Distance is : " + km);
System.out.println("The Bill is : " + bill);
}
public static void main(String args[])
{
p33 ob = new p33();
ob.input();
ob.calculate();
ob.main();
}
}
50
VARIABLE DESCRIPTION TABLE:
51
PROGRAM 34
CODE:
import java.util.*;
class p34
{
Scanner sc = new Scanner (System.in);
int pan;
String name;
double taxincome,tax,exd;
void input()
{
System.out.println("Enter The Personal Account Number");
pan=sc.nextInt();
System.out.println("Enter The Annual Taxable Income");
taxincome=sc.nextDouble();
System.out.println("Enter The Name");
name=sc.nextLine();
}
void cal()
{
if(taxincome<=250000)
{
tax = 0;
}
else if(taxincome>25000&&taxincome<=500000)
{
exd=taxincome-250000;
52
tax=exd*0.1;
}
else if(taxincome>500000&&taxincome<=1000000)
{
exd=taxincome-500000;
tax=30000+(exd*0.2);
}
else if(taxincome>1000000)
{
exd=taxincome-1000000;
tax=30000+(exd*0.2);
}
}
void display()
{
System.out.println(" The Name : " + name);
}
public static void main(String args[])
{
p34 ob = new p34();
53
ob.input();
ob.cal();
ob.display();
}
}
54
PROGRAM 35
CODE:
import java.util.*;
class p35
{
Scanner sc = new Scanner (System.in);
int bno,days,charge,d1,d2;
long phno;
String name;
void input()
{
System.out.println("Enter The Name");
name=sc.nextLine();
System.out.println("Enter The Book Number");
bno=sc.nextInt();
System.out.println("Enter The Phone Number");
phno=sc.nextLong();
System.out.println("Enter The No.Of Days");
days=sc.nextInt();
}
void compute()
{
if(days<=5)
{
charge=days*500;
}
else if(days>5&&days<=9)
55
{
d1=(days-5)*400;
charge=(5*500)+d1;
}
else if(days>9)
{
d1=(days-5)*400;
d2=(days-9)*200;
charge=(5*500)+d1+d2;
}
}
void display()
{
System.out.println(" The Name : " + name);
}
public static void main(String args[])
{
p35 ob = new p35();
56
ob.input();
ob.compute();
ob.display();
}
57
PROGRAM 36
CODE:
import java.util.*;
class p36
public class Shape
{
void polygon(int n, char ch)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(ch + " ");
}
System.out.println();
}
}
void polygon(int x, int y)
{
for (int i = 0; i < y; i++)
{
for (int j = 0; j < x; j++)
{
System.out.print("@ ");
}
System.out.println();
}
58
}
public static void main(String[] args)
{
Shape s = new Shape();
System.out.println("Square of side 4 with '*':");
s.polygon(4, '*');
System.out.println("Rectangle of 5x3 with '@':");
s.polygon(5, 3);
}
}
n int To accept and store the number of rows/columns for square polygon
59
PROGRAM 37
import java.util.*;
class p37 {
void p(int side) {
int perimeter = 4 * side;
System.out.println("Perimeter of a Square: " + perimeter);
}
60
}
}
61
PROGRAM 38
import java.util.*;
public class p38 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();
int count = 0;
int len = str.length();
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' ||
ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' ||
ch == 'O' || ch == 'U') {
count++;
}
}
System.out.println("Number of vowels: " + count);
}
}
62
Variable Description Table:
63
PROGRAM 39
import java.util.*;
public class p39 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a non-palindrome word: ");
String word = sc.nextLine();
String reverse = "";
for (int i = word.length() - 1; i >= 0; i--) {
reverse = reverse + word.charAt(i);
}
String palindrome = word + reverse;
System.out.println("Palindromic word: " + palindrome);
}
}
64
PROGRAM 40
import java.util.*;
public class p40 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();
str = str.toUpperCase();
int a = 0, e = 0, i = 0, o = 0, u = 0;
for (int j = 0; j < str.length(); j++) {
char ch = str.charAt(j);
if (ch == 'A') a++;
else if (ch == 'E') e++;
else if (ch == 'I') i++;
else if (ch == 'O') o++;
else if (ch == 'U') u++;
}
if (a > 0) System.out.println("Frequency of vowel 'A' is " + a);
if (e > 0) System.out.println("Frequency of vowel 'E' is " + e);
if (i > 0) System.out.println("Frequency of vowel 'I' is " + i);
if (o > 0) System.out.println("Frequency of vowel 'O' is " + o);
if (u > 0) System.out.println("Frequency of vowel 'U' is " + u);
}
}
65
Variable Description Table:
66
PROGRAM 41
import java.util.*;
public class p41 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Input
System.out.print("Enter a string: ");
String str = sc.nextLine();
// Convert to uppercase to handle both cases
str = str.toUpperCase();
String newStr = "";
// Loop to remove vowels
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
// Check if the character is NOT a vowel
if (ch != 'A' && ch != 'E' && ch != 'I' && ch != 'O' && ch != 'U') {
newStr = newStr + ch; // Append consonants and spaces
}
}
System.out.println("String after removing vowels: " + newStr);
}
}
67
VARIABLE DESCRIPTION TABLE:
68
PROGRAM 42
/*Write a PROGRAM in Java to accept two characters and display
the sum and difference of their ASCII value.*/
import java.util.*;
class p42
{
public static void main(String args[])
{
Scanner sc = new Scanner (System.in);
char ch,cb;
int s,d;
System.out.println("Enter two characters");
ch=sc.next().charAt(0);
cb=sc.next().charAt(0);
int h=(int)ch;
int b=(int)cb;
s=h+b;
d=h-b;
System.out.println("Sum of "+ch+" and "+cb+" : " +s);
System.out.println("Difference of "+ch+" and "+cb+" : " +d );
}
}
69
VARIABLE DESCRIPTION TABLE:
70
PROGRAM 43
import java.util.*;
class p43
{
public static void main(String args[])
{
Scanner sc = new Scanner (System.in);
char ch,cb;
int s,h;
System.out.println("Enter a charactera");
ch=sc.next().charAt(0);
h=(int)ch;
s=h+1;
cb=(char)s;
System.out.println(" The Next Alphabet in accords to "+ch+" is : " + cb);
}
}
71
VARIABLE DESCRIPTION TABLE:
72
PROGRAM 44
import java.util.*;
class p44
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a[]= new int[20];
int e=0,o=0;
{
System.out.println("Enter 20 numbers");
for(int i=0;i<20;i++)
{
a[i]=sc.nextInt();
if(a[i]%2==0)
e+=a[i];
else
o+=a[i];
}
System.out.println(" The Sum of all Even Numbers : " + e);
System.out.println(" The Sum of all Odd Numbers : " + o);
}
}
}
73
VARIABLE DESCRIPTION TABLE:
74
PROGRAM 45
import java.util.*;
public class PROGRAM45
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Year: ");
int a[] = new int[5];
int x = 0;
int f = 0;
for (int i = 0; i < a.length; i++)
{
a[i] = sc.nextInt();
}
System.out.println("Enter Year to be searched: ");
x = sc.nextInt();
for (int i = 0; i < a.length; i++)
{
if (a[i] == x)
{
System.out.println("Record Exists");
f = 1;
break;
}
}
if (f == 0) {
75
System.out.println("Record does not Exist");
}
}
}
76
PROGRAM 46
import java.util.*;
public class PROGRAM46
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int[] a = new int[10];
System.out.println("Enter 10 integers:");
int max = a[0];
int min = a[0];
for (int i = 0; i < 10; i++)
{
a[i] = sc.nextInt();
if (a[i] > max)
{
max = a[i];
}
if (a[i] < min)
{
min = a[i];
}
}
System.out.println("Maximum value: " + max);
System.out.println("Minimum value: " + min);
}
}
77
VARIABLE DESCRIPTION TABLE:
78
PROGRAM 47
import java.util.*;
class p47
{
public static void main (String args[])
{
Scanner sc = new Scanner (System.in);
int a[]=new int[10];
int s=0,i;
System.out.println("Enter 10 numbers : ");
for(i=0;i<10;i++)
{
a[i]=sc.nextInt();
s+=a[i];
}
79
VARIABLE DESCRIPTION TABLE:
80
PROGRAM 48
public class PROGRAM48
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int[] roll_no = new int[10];
String[] name = new String[10];
int[] m1 = new int[10];
int[] m2 = new int[10];
double[] percentage = new double[10];
char[] grade = new char[10];
for (int i = 0; i < 10; i++)
{
System.out.println("Roll number: ");
roll_no[i] = sc.nextInt();
sc.nextLine();
System.out.println("Name: ");
name[i] = sc.nextLine();
System.out.println("Marks in Maths: ");
m1[i] = sc.nextInt();
System.out.println("Marks in Science: ");
m2[i] = sc.nextInt();
percentage[i] = ((m1[i] + m2[i]) / 200.0) * 100.0;
if (percentage[i] >= 80)
{
grade[i] = 'A';
81
}
else if (percentage[i] >= 60)
{
grade[i] = 'B';
}
else if (percentage[i] >= 40)
85
{
grade[i] = 'C';
}
else
{
grade[i] = 'D';
}
System.out.println();
System.out.println(roll_no[i] + " " + name[i] + " " + m1[i] + " " + m2[i] + " " +
percentage[i] + " " + grade[i]);
}
}
}
82
83
84
VARIABLE DESCRIPTION TABLE:
85
PROGRAM 49
import java.util.*;
int sum = 0;
System.out.print("Name:");
name[i] =sc.nextLine();
86
System.out.print("Total marks: ");
totalmarks[i] = sc.nextInt();
sc.nextLine();
sum += totalmarks[i];
}
}
}
87
88
VARIABLE DESCRIPTION TABLE:
89
PROGRAM 50
import java.util.*;
public class p50 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Input size of the square matrix
System.out.print("Enter the size of the square matrix (n): ");
int n = sc.nextInt();
int[][] arr = new int[n][n];
// Input elements
System.out.println("Enter the elements of the matrix:");
90
}
91
PROGRAM 51
import java.util.*;
public class PROGRAM51{
public static void main(String[] args)
{
int[] a = {1982, 1987, 1995, 1996, 2003, 2006, 2007, 2009, 2010};
Scanner sc = new Scanner(System.in);
System.out.print("Enter your graduation year: ");
int x = sc.nextInt();
int l = 0;
int h = a.length - 1;
int count = 0;
if (x >= a[l] && x <= a[h])
{
do
{
int mid = (l + h) / 2;
if (a[mid] == x)
{
count = 1;
break;
}
else if (x < a[mid])
{
h = mid - 1;
}
else
92
{
l = mid + 1;
}
}
while (l <= h);
}
if (count == 1)
96
{
System.out.println("Record exists.");
}
else
{
System.out.println("Record does not exist.");
}
}
}
93
VARIABLE DESCRIPTION TABLE:
94
PROGRAM 52
import java.util.*;
public class p52 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String names[] = new String[6];
// Input
System.out.println("Enter 6 names:");
for (int i = 0; i < 6; i++) {
names[i] = sc.next();
}
// ----- Bubble Sort -----
System.out.println("\nBubble Sort:");
for (int i = 0; i < 6 - 1; i++) {
for (int j = 0; j < 6 - 1 - i; j++) {
if (names[j].compareTo(names[j + 1]) > 0) {
// Swap
String temp = names[j];
names[j] = names[j + 1];
names[j + 1] = temp;
}
}
}
for (int i = 0; i < 6; i++) {
95
// ----- Selection Sort -----
// Reset the array
names[0] = "Anto";
names[1] = "Sri";
names[2] = "Arsh";
names[3] = "Vishav";
names[4] = "Reyansh";
names[5] = "Avi";
System.out.println("\n\nSelection Sort:");
for (int i = 0; i < 6 - 1; i++) {
int min = i;
for (int j = i + 1; j < 6; j++) {
if (names[j].compareTo(names[min]) < 0) {
min = j;
}
}
// Swap
String temp = names[i];
names[i] = names[min];
names[min] = temp;
}
for (int i = 0; i < 6; i++) {
System.out.print(names[i] + " ");
}
}
}
96
VARIABLE DESCRIPTION TABLE:
97