I need a simple pr0gram in java that w0uld prints all subsets of {1,2,3, . . . ,
ID: 639394 • Letter: I
Question
I need a simple pr0gram in java that w0uld prints all subsets of {1,2,3, . . . , n}. F0r example, if m=4 then the c0de sh0uld print: {}, {1}, {2}, {3}, {4}, {1, 2}, {1, 3}, {1,4}, {2,3}, {2,4}, {3,4}, {1,2,3}, {1,2,4}, {1,3,4}, {2,3,4}, {1,2, 3,4} ....................I need to be able to specify the m for example m size of 4 just like the example above.........I wrote the program below but it is not doing what I want it to do ....it needs modification
import java.util.Scanner;
public class rough12
{
public static void main(String args[])
{
Scanner x=new Scanner(System.in);
int N;
N=x.nextInt();
int allMasks = (1 << N);
for (int i = 1; i < allMasks; i++)
{
System.out.print("{");
for (int j = 0; j < N; j++)
if ((i & (1 << j)) > 0)
// Check if jth bit in the i is set
// If set then pront jth element from set
System.out.print((j + 1) + ",");
System.out.print("},");
}
}
}
Explanation / Answer
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.