-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBarChart.java
50 lines (41 loc) · 912 Bytes
/
BarChart.java
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
43
44
45
46
47
48
49
50
import java.util.*;
class BarChart{
public static void main(String args[]) {
System.out.println("Hello World");
Scanner sc = new Scanner(System.in);
System.out.println("enter N: ");
int n = sc.nextInt();
int arr[] = new int[n];
int i=0;
System.out.println("enter the numbers: ");
while(n!=0){
arr[i] = sc.nextInt();
n--;
i++;
}
barChart(arr, n);
}
public static void barChart(int arr[], int n){
int row = maximum(arr, n);
for(int i=row; i>=1; i--){
for(int j=0; j<arr.length; j++){
if(arr[j] >= i){
System.out.print("*");
}
else{
System.out.print(" ");
}
}
System.out.println();
}
}
static int maximum(int arr[], int n){
int max = Integer.MIN_VALUE;
for(int i:arr){
if(max< i){
max = i;
}
}
return max;
}
}