473,467 Members | 1,981 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

5 New Member
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 2100
Rabbit
12,516 Recognized Expert Moderator MVP
You're missing a combination, {0,2,3,5}. What have you tried?
Apr 3 '13 #2
adri00713
5 New Member
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
Rabbit
12,516 Recognized Expert Moderator MVP
Yes, I know you have to create a method, but what have you tried so far?
Apr 3 '13 #4
adri00713
5 New Member
Expand|Select|Wrap|Line Numbers
  1. public class CombinationApp {
  2.     public static void main(String[] args) {
  3.         int[] numberlist = {1,2,3,4,5,6,7,8,9,10};
  4.         twoCombination(numberlist);
  5. }
  6.     public static int twoCombination(int[] numberlist){
  7.  
  8.         try{
  9.  
  10.         for(int x = 0;x<=numberlist.length;x++){
  11.             int fnum =numberlist[x];
  12.             int snum =numberlist[x + 1];
  13.             int sum = fnum  + snum ;
  14.             if(sum == 10){
  15.                System.out.println("{"+fnum+" , "+ snum +"}");
  16.  
  17.             }
  18.  
  19.  
  20.  
  21.  
  22.         }
  23.         }catch(ArrayIndexOutOfBoundsException e){
  24.  
  25.         }
  26.  
  27.  
  28.             return 0;
  29.         }
  30.  
  31.  
  32.  
  33.  
  34.  
  35. }
Apr 3 '13 #5
Rabbit
12,516 Recognized Expert Moderator MVP
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

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: acb | last post by:
Hi, I have a list of different objects in a <List> Structure. There is only one category of each kind of object. Current I have the following methods: public static Flag GetFlagObj() {...
9
by: Doug | last post by:
Hi I have a method that I would like to return the value to the calling event. I have a bad example below that doesnt give me what I want. What I want is to have a my readxml mthod return the...
2
by: Jay Balapa | last post by:
Hello, We have a webservice which is called by our pocket pc apps. We have a need to filter data, so we want to add a another method on top to the existing method. Existing Service.asmx...
5
by: Bails | last post by:
Hi all I have a theory for a lotto system and need help on how to code it. I want to create 1 massive database with EVERY combination of numbers possible in a given lotto system, then remove all...
10
by: deppeler | last post by:
I am wanting to return the number of records (rows) in a flat file db. My script is a resource kit library and each item has a catalog number ($catnumber), instead of the user having to remember the...
4
by: Robba | last post by:
I got a little problem, i have to return the number of the array of the word that is filled in. First there is filled in a sentence, and have to be split. I think i splitted it but now i cant...
9
by: =?Utf-8?B?bGlhbnF0bGl0?= | last post by:
best way to use: an out method parameter or a method with return ?
10
by: =?Utf-8?B?bGlhbnF0bGl0?= | last post by:
In performance wise which is more costly to use when there one value to return? Is it the out method parameter or the method that has a return value?
6
guillermobytes
by: guillermobytes | last post by:
Hi, I was wondering if there is a way (from an interface or abstract class) to force all classes implementing some method to return a specific type, like in Java. example : //method can only...
1
by: Fred Roach | last post by:
I'm trying to write a macro that will keep track of sales/commissions. I have written a simple macro that will ask the user for data using input boxes, and place that data into an inserted line. The...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.