logo
  • Home
  • Rank
  • Tests
  • About
  • Training
  • Programming
  • CS
  • IT
  • IS
  • ECE
  • EEE
  • EE
  • Civil
  • Mechanical
  • Chemical
  • Metallurgy
  • Instrumentation
  • Aeronautical
  • Aerospace
  • Biotechnology
  • Agriculture
  • MCA
  • BCA
  • Internship
  • Contact

Questions & Answers

C Interview Questions
C++ Questions
Linux MCQs
C# Quiz
Java MCQs
JavaScript MCQs
SAN Questions
PHP Questions
Python Quiz

Computer Science Questions

Operating System Quiz
Computer Architecture MCQs
Software Architecture MCQs
Software Engineering MCQs
Artificial Intelligence MCQs
LISP Programming MCQs
Database Management MCQs
Computer Network MCQs
Microprocessor MCQs

C Programming Examples

Simple C Programs
C - Arrays
C - Matrix
C - Strings
C - Bitwise Operations
C - Linked Lists
C - Stacks & Queues
C - Searching & Sorting
C - Trees
C - Strings
C - File Handling
C - Mathematical Functions
C - Puzzles & Games
C Programs - Recursion
C Programs - No Recursion

Java Algorithms

Java - Numerical Problems
Java - Combinatorial Problems
Java - Graph Problems
Java - Hard Graph Problems
Java - Computation Geometry
Java - Sets & Strings
Java - Data-Structures
Java - Collection API Problems

C++ Algorithms

C++ - Numerical Problems
C++ - Combinatorial Problems
C++ - Graph Problems
C++ - Hard Graph Problems
C++ - Computation Geometry
C++ - Sets & Strings
C++ - Data-Structures
C++ - STL Library

C Algorithms

C - Numerical Problems
C - Combinatorial Problems
C - Graph Problems
C - Hard Graph Problems
C - Computation Geometry
C - Sets & Strings
C - Data-Structures

« Prev Page
Next Page »

Java Program to Find Two Elements such that their Sum is Closest to Zero

Posted on November 15, 2014 by javaguy
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.

advertisement
  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

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement

Here’s the list of Best Reference Books in Java Programming, Data Structures and Algorithms.

« Prev Page - Java Program to Find Largest Between Three Numbers Using Ternary Operator
» Next Page - Java Program to Find Out the Number of Objects Created of a Class

« Java Program to Find Arithmetic Sum of 2 or 3 or 4 Variables by Passing Argument using Method Overloading
Java Program to Find Out the Number of Objects Created of a Class »

Deep Dive @ Sanfoundry:

  1. Java Programming Examples on Graph Problems & Algorithms
  2. C# Programming Examples on Arrays
  3. C++ Programming Examples on Combinatorial Problems & Algorithms
  4. Java Programming Examples on Numerical Problems & Algorithms
  5. Java Programming Examples on Data-Structures
  6. Java Programming Examples on Collection API
  7. C Programming Examples on Combinatorial Problems & Algorithms
  8. Java Programming Examples on Hard Graph Problems & Algorithms
  9. C Programming Examples on Arrays
  10. Java Programming Examples on Combinatorial Problems & Algorithms
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below:
LinkedIn | Facebook | Twitter | Google+

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos
Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
Terms
Privacy Policy
Jobs
Bangalore Training
Online Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry. All Rights Reserved.