Type Here to Get Search Results !

Plus Minus in java using Functions and Arrays.

0

 Plus Minus in java using Functions and Arrays.


Calculate plus minus in java programming using functions and arrays of java.

Question :-

calculate the ratios of its elements that are positive, negative, and zero. Print the decimal value of each fraction on a new line.

Input Format :-

The first line contains an integer, N, the size of the array. The second line contains N space-separated integers of the array.

Output Format :-

Print the following 3 lines, each to decimals (use double in java):

proportion of positive values

proportion of negative values

proportion of zeros

Java Program :-

import java.io.*;
import java.util.*;
public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner (System.in);
        int n = sc.nextInt();
        double negative=0;
        double positive=0;
        double zeroes=0;

        for(int i=0;i<n;i++)
        {
            int data = sc.nextInt();
            if(data<0)
            {
                negative++;
            } else if (data>0)
            {
                positive++;
            } else
            {
                zeroes++;
            }
        }
        double total =n;
        System.out.println(positive/total);
        System.out.println(negative/total);
        System.out.println(zeroes/total);
    }
}

Sample Input :-

6
-4 3 -9 -5 4 1   

Sample Output :-

0.5
0.5
0

Explanation :-

There are 3 positive numbers, 3 negative numbers, and no zero in the array.

The proportions of occurrence are positive: 0.5, negative: 0.5 and zeros: 0.

In case you are solving in C++, setprecision to 3

Post a Comment

0 Comments