Write a Java Program to Find the Integer number in given array that is closest to zero

find-the-integer-number-in-given-array-that-is-closest-to-zero

Hi, Welcome to the Practicehouse. It’s one of the most common Java interview questions even I got this from also the same source “HIRING TEAM”. As you all know already I shared my personal experience with you guy’s in this practice house.

Let’s talk about this post. In this post, I am gonna share with you how to write a Java program to find the Integer Number in a given array that is closest to zero. Mentioned below is the snippet from a hiring team.

In the question, they stated that we have to implement a method to return the integer number in a given array that is closest to zero. Read carefully they also mentioned that if a number is positive and negative we should pick the positive number.

 

Clone / Download the Sample Project from GitHub :  Download / Clone Sample Project

 

Small Explanation:

Feel Free to Skip. it’s optional. we will loop through the given array and we will check with the first value multiplication. If the array’s value is less than or equal to first value then array’s value will be the closest value.

If your question why need to multiply we can directly compare with our array’s value. but it will give you a negative output. That’s why need to multiply to compare then u will get the positive output.

 

Code Sample:

Sample Output:

Thanks for choosing us from your google search. We appreciate your intention. If you want to cover us some more complex interview questions related to Java, Spring, Hibernate, and SQL. Please let us know in the comment section. we will solve it for you.

 

Leave a Reply

avatar
  Subscribe  
Notify of