In: Computer Science
Java - Design and implement an application that creates a histogram that allows you to visually inspect the frequency distribution of a set of values. The program should read in an arbitrary number of integers that are in the range 1 to 100 inclusive; then produce a chart similar to the one below that indicates how many input values fell in the range 1 to 10, 11 to 20, and so on. Print one asterisk for each value entered.
Sample Output:
1 - 10 | *****
11 - 20 | *******
21 - 30 | *
31 - 40 | *
41 - 50 | *
51 - 60 | *
61 - 70 | *
71 - 80 | *
81 - 90 | *
91 - 100 | *
Thank you!
import java.util.*;
public class Main
{
public static void main(String[] args)
{
//Scanner
Scanner input = new Scanner(System.in);
//array declaration
int values[] = new int[100];
//variable declaration
int size=0, num;
//while loop
while(true)
{
//get user input
System.out.print("Enter a
number(1:100 and 0 for Exit): ");
num = input.nextInt();
if(num<1 || num>100)
break;
else
values[size++] = num;
}
//display the histogram
System.out.println("\nThe histogram is given below:
");
for(int i=1; i<100; i=i+10)
{
System.out.print("\n"+i+" - "+(i+9)+" | ");
for(int j=0; j<size; j++)
{
if(values[j]>=i &&
values[j]<=(i+9))
System.out.print("*");
}
}
}
}
OUTPUT:
Enter a number(1:100 and 0 for Exit): 5
Enter a number(1:100 and 0 for Exit): 18
Enter a number(1:100 and 0 for Exit): 6
Enter a number(1:100 and 0 for Exit): 13
Enter a number(1:100 and 0 for Exit): 7
Enter a number(1:100 and 0 for Exit): 6
Enter a number(1:100 and 0 for Exit): 9
Enter a number(1:100 and 0 for Exit): 15
Enter a number(1:100 and 0 for Exit): 16
Enter a number(1:100 and 0 for Exit): 18
Enter a number(1:100 and 0 for Exit): 14
Enter a number(1:100 and 0 for Exit): 13
Enter a number(1:100 and 0 for Exit): 56
Enter a number(1:100 and 0 for Exit): 36
Enter a number(1:100 and 0 for Exit): 25
Enter a number(1:100 and 0 for Exit): 45
Enter a number(1:100 and 0 for Exit): 34
Enter a number(1:100 and 0 for Exit): 55
Enter a number(1:100 and 0 for Exit): 66
Enter a number(1:100 and 0 for Exit): 77
Enter a number(1:100 and 0 for Exit): 88
Enter a number(1:100 and 0 for Exit): 99
Enter a number(1:100 and 0 for Exit): 0
The histogram is given below:
1 - 10 | *****
11 - 20 | *******
21 - 30 | *
31 - 40 | **
41 - 50 | *
51 - 60 | **
61 - 70 | *
71 - 80 | *
81 - 90 | *
91 - 100 | *