Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Quearn

Quearn Logo Quearn Logo

Quearn Navigation

  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Quearn Academy
  • About Us
  • Blog
  • Contact Us
Home/Questions/Q 803884
Next
In Process
codemania
  • 0
codemania
Asked: May 27, 20232023-05-27T09:29:16+00:00 2023-05-27T09:29:16+00:00In: Education

Java Program To Find Two Elements whose Sum is Closest to Zero

  • 0
Java Program To Find Two Elements whose Sum is Closest to Zero
codejava
  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    1 Answer

    1. codemania
      2023-05-27T09:29:29+00:00Added an answer on May 27, 2023 at 9:29 am
      This is a Java Program to Find Two Elements such that their Sum is Closest to Zero. 

      Enter any integer number as an input which denotes the size of the array. After that add all the required integers in the array. Now we pick all all two possible pairs and compare their sum with the minumum possible sum closest to zero. Since sum can be negative so we use absolute function to get only positive result. Hence the two integers with sum closest to zero are output.

      Here is the source code of the Java Program to Find Two Elements such that their Sum is Closest to Zero. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

      1. import java.util.Scanner;
      2. public class Sum_Zero
      3. {
      4.     public static void main(String[] args)
      5.     {
      6.         int n, min1 = 0, min2 = 1, sum, minimum;
      7.         Scanner s = new Scanner(System.in);
      8.         System.out.print("Enter the number of elements you want:");
      9.         n = s.nextInt();
      10.         int a[] = new int[n];
      11.         System.out.println("Enter all the numbers:");
      12.         for(int i = 0; i < n; i++)
      13.         {
      14.             a[i] = s.nextInt();
      15.         }
      16.         minimum=Math.abs(a[0] + a[1]);
      17.         for(int i = 0; i < n; i++)
      18.         {
      19.             for(int j = i + 1; j < n; j++)
      20.             {
      21.                 sum = Math.abs(a[i] + a[j]);
      22.                 if(sum < minimum)
      23.                 {
      24.                     min1 = i;
      25.                     min2 = j;
      26.                     minimum = sum;
      27.                 }
      28.             }
      29.         }
      30.         System.out.println("Element 1:"+a[min1]);
      31.         System.out.println("Element 2:"+a[min2]);
      32.     }
      33. }

      Output:

      $ javac Sum_Zero.java
      $ java Sum_Zero
       
      Enter the number of elements you want:5
      Enter all the numbers:
      -2
      -1
      3
      6
      5
      Element 1:-2
      Element 2:3
      • 0
      • Share
        Share
        • Share onFacebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    You must login to add an answer.

    Forgot Password?

    Need An Account, Sign Up Here

    Related Questions

    • Codility Passing Car Problem in Java
    • Dutch National Flag Problem in Java
    • Java Program to Check if an Array is Decreasing
    • Java Program to Check if an Array is Increasing
    • Smallest Sum Contiguous Subarray in Java

    Sidebar

    Ask A Question
    • Popular
    • Answers
    • imran

      Point out the wrong statement. A. The standard instances are ...

      • 217 Answers
    • imran

      Point out the correct statement. A. A volume is mounted ...

      • 204 Answers
    • VARISHA PARVEZ

      Which type of e‐commerce focuses on consumers dealing with ...

      • 180 Answers
    • Varsha
      Varsha added an answer B. Dumb terminal May 28, 2023 at 10:55 am
    • Mahak Chopra
      Mahak Chopra added an answer a) error reporting May 28, 2023 at 10:55 am
    • Shivangi
      Shivangi added an answer A.promotion of collective bargaining May 28, 2023 at 10:55 am

    Related Questions

    • What are the types of distributed operating systems? a) Zone ...

      • 0 Answers
    • Network operating system runs on ___________ a) every system in ...

      • 1 Answer
    • Which principle states that programs, users, and even the systems ...

      • 1 Answer
    • In SCSI disks used in high end PCs, the controller ...

      • 1 Answer
    • The _______ program initializes all aspects of the system, from ...

      • 1 Answer

    Top Members

    Siya08

    Siya08

    • 0 Questions
    • 0 Answers
    Tanvi R

    Tanvi R

    • 0 Questions
    • 0 Answers
    Dhanashri

    Dhanashri

    • 0 Questions
    • 0 Answers

    Trending Tags

    account algorithm analytics answer bank c computer computer science python cs e commerce electric electronics it language mcq mechanical physics programming python question

    Explore

    • Home
    • Quearn Academy
    • Add group
    • Groups page
    • Communities
    • Questions
      • New Questions
      • Trending Questions
      • Must read Questions
      • Hot Questions
    • Polls
    • Tags
    • Badges
    • Users
    • Help

    Footer

    Quearn

    Quearn is a social questions & Answers Engine which will help you establish your community and connect with other people, Also you Can Earn Money.

    About Us

    • Meet The Team
    • Blog
    • About Us
    • Contact Us

    Legal Stuff

    • Terms of Use
    • Privacy Policy
    • Cookie Policy

    Help

    • Knowledge Base
    • Support

    Follow

    © 2018-2023 Quearn®. All Rights Reserved | Developed by ASHAS Industries Proudly 🇮🇳