ICSE Java Programming Range of Array 2023 Q6 SPECIMEN PAPER
Range of array in java example.
import java.util.Scanner;
public class ICSEJava2023 {
public static void main(String[] args) {
Range obj = new Range();
obj.inputArray();
System.out.println("Original Array;");
obj.display();
obj.sortArray();
}
}
class Range {
public void sortArray() {
for (int i = 0; i < this.array_size - 1; i++)
for (int j = 0; j < this.array_size - i - 1; j++)
if (this.array[j] < this.array[j + 1]) {
double temp = this.array[j];
this.array[j] = this.array[j + 1];
this.array[j + 1] = temp;
}
System.out.println("Range of the array is:"+(this.array[0]-this.array[this.array_size-1]));
}
public void inputArray() {
Scanner in = new Scanner(System.in);
System.out.println("Enter the elements of the array:");
for (int i = 0; i < this.array_size; i++) {
System.out.println("Enter the element:");
this.array[i] = in.nextDouble();
}
in.close();
}
public void display() {
for (int i = 0; i < this.array_size; i++)
System.out.print(this.array[i] + " ");
System.out.println();
}
Range() {
this.range = 0;
}
private double[] array = new double[20];
private double range;
private static int array_size = 20;
}
No comments:
Post a Comment