Multiply Negative numbers in java

For this challenge, you will be given an array and you are asked to count how many numbers are negative and multiply them and print the output to the STDOUT. 


Input Format - 
you will be taking a number as an input from STDIN which tells about the length of the array. On another line, array elements should be there with single space between them. 

Output Format 
print the multiplication to the STDOUT. 


/*
* For this challenge, you will be given an array and you are asked to count
* how many numbers are negative and multiply them and print the output to the STDOUT.
*/
import java.io.*;
import java.util.*;
public class CandidateCode {
public static void main(String args[] ) throws Exception {
Scanner sc=new Scanner(System.in);
int size;
size=sc.nextInt();
int [] arr=new int[size];
for(int i=0;i<size;i++){
arr[i]=sc.nextInt();
}
int product=1;
for(int j=0;j<arr.length;j++){
if(arr[j]<0){
product*=arr[j];
}
}
System.out.println(product);
sc.close();
}
}
Sample Test Case: 
Sample Input:-7
-8 -7 6 0 7 -1 6

Sample Output 
-56 

Explanation: 
There are three negative numbers present in the array and their multiplication comes out to be -56.

Comments

Post a Comment

Popular posts from this blog

Reasoning-Number Series

Profit and Loss

Reasoning-Letter Series