for Input N=5
Output : Given number 5
Combinations :
1 1 1 1 1
1 1 1 2
1 2 2
1 1 3
1 4
2 3
Output : Given number 5
Combinations :
1 1 1 1 1
1 1 1 2
1 2 2
1 1 3
1 4
2 3
import java.io.*;
class numcombi
{
String str="";
int a=0;
int b=0;
public void combi() throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a number : ");
int num = Integer.parseInt(br.readLine());
System.out.println("Combinations :");
for(int i=1;i<=num/2;i++)
{
str="";
extract(num,i);
}
}
public void extract(int n, int m)
{
a=m;
str+=" "+a;
b=n-a;
System.out.println(str+" "+b);
if(b<=m)
{
return;
}
extract(b,m);
}
public static void main(String arg[ ]) throws IOException
{
numcombi obj= new numcombi();
obj.combi();
}
}
No comments:
Post a Comment