Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
GreenGeeks Web Hosting - Faster, Scalable & Eco-Friendly

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Distribute your Resume to 500+ Job Boards & Recruiters


7,788 questions

10,874 answers

573 users

How to find and print pairs in int array that equal to target number in Java

1 Answer

0 votes
package javaapplication1;

public class JavaApplication1 {
  
    public static void main(String[] args) {

        int[] arr = {1, 2, 3, 4, 5, 6, 3, 1, 2};
       
        findAndPrintPairsEqualtoTarget(arr, 3);
    }
    
    public static void findAndPrintPairsEqualtoTarget(int[] array, int target) {	
	   for (int i = 0; i < array.length; i++)
	   {	
            for (int j = i + 1; j < array.length; j++)
            {	
                if (array[i] + array[j] == target)
                    System.out.println("indexes " + i + " and " + j
						               + " are " + array[i] + " + " + array[j] 
                                       + " = " + target);
            }
	   }
    }
}
    
/*
run:
   
indexes 0 and 1 are 1 + 2 = 3
indexes 0 and 8 are 1 + 2 = 3
indexes 1 and 7 are 2 + 1 = 3
indexes 7 and 8 are 1 + 2 = 3
    
*/

 




answered Jun 29, 2017 by avibootz
...