454,654 Members | 1,517 Online Need help? Post your question and get tips & solutions from a community of 454,654 IT Pros & Developers. It's quick & easy.

# method to returns all possible number combinations and their sum is equal to a number

 P: 5 Given the list of a maximum of 10 numbers, where all numbers are different in the list create a method that will return all possible combinations in each combination the of the numbers should be equal to 10. example: input number list {0,2,3,5,10} output is {2,3,5} {0,10} Apr 3 '13 #1
5 Replies

 Expert Mod 10K+ P: 12,421 You're missing a combination, {0,2,3,5}. What have you tried? Apr 3 '13 #2

 P: 5 yeah i know that the possible number of combinations are 2 digits 3 digits 4 digits i need to create a method .. Apr 3 '13 #3

 Expert Mod 10K+ P: 12,421 Yes, I know you have to create a method, but what have you tried so far? Apr 3 '13 #4

 P: 5 Expand|Select|Wrap|Line Numbers public class CombinationApp {     public static void main(String[] args) {         int[] numberlist = {1,2,3,4,5,6,7,8,9,10};         twoCombination(numberlist); }     public static int twoCombination(int[] numberlist){           try{           for(int x = 0;x<=numberlist.length;x++){             int fnum =numberlist[x];             int snum =numberlist[x + 1];             int sum = fnum  + snum ;             if(sum == 10){                System.out.println("{"+fnum+" , "+ snum +"}");               }                 }         }catch(ArrayIndexOutOfBoundsException e){           }                 return 0;         }           } Apr 3 '13 #5

 Expert Mod 10K+ P: 12,421 All that does it add two numbers to see if the sum is 10. What you need to do is loop through every combination of numbers to see if it adds up to 10. To that end you will need to use at least 2 loops. One nested in the other. Apr 3 '13 #6 