Home >>Cpp Programs >C++ Program to print all possible subset of a set

C++ Program to print all possible subset of a set

C++ Program to print all possible subset of a set

In this example, we will see a C++ program through which we can print all the possible subset of a given set.

The total number of possible subsets a given set can have is 2^n.

where n is the number of elements present in that given set.

Program:

#include <bits/stdc++.h>
using namespace std;
void allPossibleSubset(int arr[],int n)
{
int  count = pow(2,n);
// The outer for loop will run 2^n times to print all subset .
// Here variable i will act as a binary counter
for (int i = 0; i < count; i++)
{
// The inner for loop will run n times , As the maximum number of elements a set can have is n
// This loop will generate a subset
for (int j = 0; j < n; j++)
{
// This if condition will check if jth bit in binary representation of  i  is set or not
// if the value of (i & (1 << j)) is greater than 0 , include arr[j] in the current subset
// otherwise exclude arr[j]
if ((i & (1 << j)) > 0)
cout << arr[j] << " ";
}
cout << "\n";
}
}
int main()
{
int n;
cout << "Enter size of the set\n";
cin >> n;
int arr[n];
cout << "Enter Elements of the set\n";
for(int i=0;i<n;i++)
cin >> arr[i];
allPossibleSubset(arr,n);
return 0;
}

Output:
Enter size of the set
5
Enter Elements of the set
1
2
3
4
5

1 
2 
1 2 
3 
1 3 
2 3 
1 2 3 
4 
1 4 
2 4 
1 2 4 
3 4 
1 3 4 
2 3 4 
1 2 3 4 
5 
1 5 
2 5 
1 2 5 
3 5 
1 3 5 
2 3 5 
1 2 3 5 
4 5 
1 4 5 
2 4 5 
1 2 4 5 
3 4 5 
1 3 4 5 
2 3 4 5 
1 2 3 4 5 

Cpp Programs Fibonacci Series in C++ Armstrong Number in C++ Factorial program in C++ Check Palindrome in C++ Prime Number Program In C++ Reverse Number Program in C++ Sum Of Digits Program in C++ C++ Find C++ protected keyword CPP Program for different ways to print array elements CPP Program to determine the colour of chess square CPP Program to Reverse Number CPP Program to Calculate Power of a Number CPP Program to print all Even and Odd numbers from 1 to N Program to find whether a no is power of two in CPP C++ program to find largest list of prime numbers Auto keyword in Cpp C++ program to print the left Rotation of the array Convert a given binary Tree to Doubly Linked List Delete keys in a Linked list using C++ program How do you delete a linked list in C++ Implement stack with linked list in c++ C++ Program to find first occurrence of a Number Using Recursion in an array C++ program to find Last occurrence of a Number using Recursion in an array C++ Program to find Union of two singly Linked Lists Remove Duplicates from linked list in c++ C++ Program to find Nth node in Linked List Merge sort singly linked list c++ C++ Program to Convert Roman Number to Integer Number C++ Program to find LCM of two numbers C++ Password Generator C++ Program to multiply two numbers without using multiplication operator C++ Program to print all possible subset of a set Sum of all the elements in an array divisible by a given number Print First uppercase letter in a string c++ C++ Program to Find the Number Occurring Odd Number of Times C++ Program for Print address of Variable Using Pointer C++ Program to Find ASCII Value of a Character C++ Classes and Objects Program Create a class method in C++ C++ Create Empty Class Define a class method outside the class definition in C++ How to create multiple objects of a class in C++
No Sidebar ads