Results 1 to 5 of 5
- 05-25-2011, 08:25 AM #1
Newbies
- Join Date
- May 2011
- Posts
- 3
- Rep Power
- 0
Possible combinations for a n- digit number???
Hai Gurus,
Can somebody suggest ,me the logic or the code to get the generalised form for finding the possible combinations for a a n-digit number?
Ex: If I input the number as 1234 it should give the output as all the possible 4! combinations that can be derived by the 4 digits in it.
Here is my work on the 3- digit number!! :p
import java.io.*;
class x5
{
public static void main(String args[])throws IOException
{
int x;
System.out.print("\n Enter the 3 digit number:");
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
x=Integer.parseInt(br.readLine());//568
int a=x/100; //5
int b=x/10; //56
b=b%10;//6
int c=x%10;//8
System.out.println("Possible combinations using the number "+x+ " are:");
System.out.println(((a*100)+(b*10)+c)+" "+((a*100)+(c*10)+b)+" "+(a+(b*10)+(c*100))+" "+((a*10)+b+(c*100))+" "+((b*100)+(a*10)+c)+" "+((b*100)+(c*10)+a));
}
}
- 05-25-2011, 08:45 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You are looking to find all the possible permutations of an n digit number?
Show us an example of a 3 digit number worked out by hand, let's say 2, 3, 4. Try to look for a pattern. To found out how many actual combinations an n digit number can produce, try with a few small numbers by hand and look for a pattern.
Also, please use code tags, [code] YOUR CODE HERE [/code]
- 05-25-2011, 08:49 AM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Also, as a bit of advice it would be helpful to create some helper methods, one can take some n digit number and split it into an n digit array(either mathematically, or via string conversion) and another helper method can take an array and generate a list of all possible permutations.
- 05-25-2011, 08:55 AM #4
Newbies
- Join Date
- May 2011
- Posts
- 3
- Rep Power
- 0
I did that...
But while trying for the four digit number we have to ensure that fpr each digit we repeat the 3- digit code...
But I m lacking on how to generalise it... We can derive answers if we want to manually write the combinations for the 4- digit code but we want to generalise it!!
- 05-25-2011, 08:55 AM #5
Newbies
- Join Date
- May 2011
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
User Enters 4-Digit Integer, Console Returns 1 Digit Per Line
By STANGMMX in forum New To JavaReplies: 0Last Post: 01-23-2011, 12:37 AM -
4 digit restriction
By GTM in forum Java AppletsReplies: 3Last Post: 12-28-2010, 02:26 AM -
select a digit in a number
By navid in forum New To JavaReplies: 3Last Post: 12-12-2010, 10:47 AM -
All possible combinations given a binary number
By LeanA in forum New To JavaReplies: 9Last Post: 06-18-2010, 05:33 PM -
convert getValue result in a 4 digit number
By roseline43 in forum New To JavaReplies: 0Last Post: 09-02-2008, 08:44 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks