Write a java program that specifies three parallel one dimensional arrays name length, width, and area. Each array should be capable of holding a number elements provided by user input. Using a for loop input values for length and width arrays. The entries in the area arrays should be the corresponding values in the length and width arrays (thus, area[i] = length [i]* width [i]) after data has been entered display the following output:
import java.util.Scanner;
public class App {
public static void main(String[] args) {
Scanner keyBoard = new Scanner(System.in);
System.out.print("Enter the number of rectangles: ");
int n = keyBoard.nextInt();
double[] length = new double[n];
double[] width = new double[n];
double[] area = new double[n];
for (int i = 0; i < n; i++) {
System.out.print("Enter the length for Rectangle " + (i + 1) + ": ");
length[i] = keyBoard.nextDouble();
System.out.print("Enter the width for Rectangle " + (i + 1) + ": ");
width[i] = keyBoard.nextDouble();
area[i] = length[i] * width[i];
}
System.out.println("Length\tWidth\tArea");
System.out.println("------\t-----\t----");
for (int i = 0; i < n; i++) {
System.out.println(length[i] + "\t" + width[i] + "\t" + area[i]);
}
keyBoard.close();
}
}
Comments
Leave a comment