Question:
How to print a histogram using an array?
Code:
public class Histogram { public static void main(String[] args) { int array[] = {4,10,7,15,12,3,7,10,6,19}; String output = "Element\tValue\tHistogram"; for ( int counter = 0; counter < array.length; counter++ ) { output += "\n" + counter + "\t" + array[ counter ] + "\t"; for ( int stars = 0; stars < array[ counter ]; stars++ ) { output += "*"; } } System.out.println(output); } }
Output:
$ java Histogram Element Value Histogram 0 4 **** 1 10 ********** 2 7 ******* 3 15 *************** 4 12 ************ 5 3 *** 6 7 ******* 7 10 ********** 8 6 ****** 9 19 *******************