-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbar-chart.java
More file actions
42 lines (30 loc) · 989 Bytes
/
bar-chart.java
File metadata and controls
42 lines (30 loc) · 989 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Bar Chart program
import java.io.*;
import java.util.*;
public class Main{
// Function to print Bar Chart
public static void barChart(int arr[], int max){
for(int line=max; line>=1; line--){
for(int j=0; j<arr.length; j++){
if(arr[j] >= line)
System.out.print("*\t");
else
System.out.print("\t");
}
System.out.println();
}
}
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int[n];
int max = Integer.MIN_VALUE;
// taking input & finding max value
for(int i=0; i<arr.length; i++){
arr[i] = sc.nextInt();
if(arr[i] > max) max = arr[i];
}
// printing bar chart
barChart(arr, max);
}
}