193k views
2 votes
5-16) (Bar Chart Printing Program) One interesting application of computers is to display graphs and bar charts. Write an application that reads five numbers between 1 and 30. For each number that’s read, your program should display the same number of adjacent asterisks. For example, if your program reads the number 7, it should display *******. Display the bars of asterisks after you read all five numbers.

User Prasadika
by
4.5k points

1 Answer

2 votes

Answer:

public class BarChartPrinting {

public static void main (String [] args) {

int numbers[] = new int[5];

Scanner input = new Scanner(System.in);

for (int i = 0; i < 5; i++){

System.out.print("Enter a number between 1 to 30: ");

numbers[i] = input.nextInt();

}

for (int i = 0; i < 5; i++){

for (int j = 0; j < numbers[i]; j++){

System.out.print("*");

}

System.out.println("");

}

}

}

Step-by-step explanation:

- Initialize the numbers array to hold the numbers

- Inside the for loop, get the numbers from the user and put them in the numbers

- Inside the nested for loop, print the asterisks, depending on the number that is in the numbers array

The outer loop will iterate five times corresponding to the each number in the array, and inner loop will print the asterisks depending on the value of the number

User Mukesh Ram
by
4.5k points