-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlusMinus12.java
54 lines (41 loc) · 1.69 KB
/
PlusMinus12.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
51
52
53
54
package MountBlue_01;
import java.util.Scanner;
public class PlusMinus12 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = input.nextInt();
}
plusMinus(arr, n);
}
static void plusMinus(int[] arr, int n) {
// we will declare and initialize 3 variables with zero this will use to store count the number of positive, negative and zeroes in array
int positiveCount = 0;
int negativeCount = 0;
int zeroesCount = 0;
// Then will iterate over an array and count and the count the number positive, negative and zero elements in array
for (int i = 0; i < n; i++) {
if(arr[i] > 0){
positiveCount++;
} else if(arr[i] < 0){
negativeCount++;
} else{
zeroesCount++;
}
}
// Now we have the count, will divide by it counts by size of an array to find the proportion of occurrence of positive, negative and zero values and prints it
// for precision we will use String.format method
System.out.println(String.format("%.6f", (double) positiveCount / n));
System.out.println(String.format("%.6f", (double) negativeCount / n));
System.out.println(String.format("%.6f", (double) zeroesCount / n));
}
}
/*
We can use String.format() method to format the decimal number to some specific
format.
Syntax:
String.format("%.Df", decimalValue);
where D is the number required number of Decimal places.
*/