Hi

What is the best way to go forward with finding all possible combinations of

nCr

Eg:

5C3 for the number 12345

123

134

135

145

234

235

345

Like that

thanks

newcoder310 0 Junior Poster in Training

Hi

What is the best way to go forward with finding all possible combinations of

nCr

Eg:

5C3 for the number 12345

123

134

135

145

234

235

345

Like that

thanks

Taimoor Rana 0 Light Poster

I picked this up online hope this can help.

Calculate the combinations for C(n,r) = n! / r! (n - r)!. For r <= n, n >= 0, and r >= 0.

newcoder310 0 Junior Poster in Training

newcoder310 0 Junior Poster in Training

This is the code I wrote for printing if the value of r is 3

```
import java.util.Scanner;
public class ncr
{
public static void main(String args[])
{
//Scanner s =new Scanner(System.in);
//int n,r;
//System.out.println("Eneter the value of n");
//n=s.nextInt();
//System.out.println("Enter the value of r");
//r=s.nextInt();
//System.out.println("Enter the number");
String[] number = {"1","2","3","4","5"};
for(int i=0;i<number.length;i++)
{
for(int j=i+1;j<number.length-1;j++)
{
int k=j+1;
System.out.println(number[i]+""+number[j]+""+number[(j+1)]);
}
}
}
}
```

I want to enhance this to get for any r using the same logic.......can u guys give any idea

Edited by mike_2000_17 because: * Fixed formatting *

newcoder310 0 Junior Poster in Training

newcoder310 0 Junior Poster in Training

well even if its not the same logic its ok

ztini

Is your question regarding implementing the algorithm or writing the algorithm?

Hint:

This is a well established math algorithm; so why not take one of the existing methods and convert it to Java?

newcoder310 0 Junior Poster in Training

newcoder310 0 Junior Poster in Training

n!/r!(n-r)!

ztini

newcoder310 0 Junior Poster in Training

newcoder310 0 Junior Poster in Training

if u know a link kindly post it and im pretty sure i can figure it out from there

merci beacoup

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.