Given a natural number **n**, print all the subsets of the set without using any array or loop (only the use of **recursion** is allowed).

**Examples:**

Input : n = 4 Output : { 1 2 3 4 } { 1 2 3 } { 1 2 4 } { 1 2 } { 1 3 4 } { 1 3 } { 1 4 } { 1 } { 2 3 4 } { 2 3 } { 2 4 } { 2 } { 3 4 } { 3 } { 4 } { } Input : n = 2 Output : { 1 2 } { 1 } { 2 } { }

**Approach:**

- Start from upto 0.
- Consider the binary representation of
**num**with**n**bits. - Start from the leftmost bit which represents 1, the second bit represents 2 and so on until
**nth**bit which represents**n**. - Print the number corresponding to the bit if it is set.
- Perform the above steps for all values of
**num**until it is equal to 0.

Let’s understand the above approach through an example:

Considering input n = 4, start from .

and so on … until num = 0.

Below is the implementation of the above approach:

`# Python3 code to print all subsets ` `# of {1, 2, 3, …n} without using ` `# array or loop, just recursion. ` ` ` `# This recursive function calls subset ` `# function to print the subsets one by one. ` `# numBits --> number of bits needed to ` `# represent the number (simply input value n). ` `# num --> Initially equal to 2 ^ n - 1 and ` `# decreases by 1 every recursion until 0. ` `def` `printSubsets(numOfBits, num): ` ` ` ` ` `if` `num >` `=` `0` `: ` ` ` `print` `(` `"{"` `, end ` `=` `" "` `) ` ` ` ` ` `# Print the subset corresponding to ` ` ` `# binary representation of num. ` ` ` `subset(numOfBits` `-` `1` `, num, numOfBits) ` ` ` `print` `(` `"}"` `) ` ` ` ` ` `# Call the function recursively to ` ` ` `# print the next subset. ` ` ` `printSubsets(numOfBits, num` `-` `1` `) ` ` ` ` ` `else` `: ` ` ` `return` ` ` `# This function recursively prints the ` `# subset corresponding to the binary ` `# representation of num. ` `# nthBit --> nth bit from right side ` `# starting from n and decreases until 0. ` `def` `subset(nthBit, num, numOfBits): ` ` ` ` ` `if` `nthBit >` `=` `0` `: ` ` ` ` ` `# Print number in given subset only ` ` ` `# if the bit corresponding to it ` ` ` `# is set in num. ` ` ` `if` `num & (` `1` `<< nthBit) !` `=` `0` `: ` ` ` `print` `(numOfBits ` `-` `nthBit, end ` `=` `" "` `) ` ` ` ` ` `# Check for the next bit ` ` ` `subset(nthBit` `-` `1` `, num, numOfBits) ` ` ` ` ` `else` `: ` ` ` `return` ` ` `# Driver Code ` `n ` `=` `4` `printSubsets(n, ` `2` `*` `*` `n ` `-` `1` `) ` |

*chevron_right*

*filter_none*

**Output:**

{ 1 2 3 4 } { 1 2 3 } { 1 2 4 } { 1 2 } { 1 3 4 } { 1 3 } { 1 4 } { 1 } { 2 3 4 } { 2 3 } { 2 4 } { 2 } { 3 4 } { 3 } { 4 } { }

**Time Complexity:**

## Recommended Posts:

- Subset array sum by generating all the subsets
- Maximum possible difference of two subsets of an array
- How to hide an element when printing a web page using CSS?
- Subsets having Sum between A and B
- Given a set, find XOR of the XOR's of all subsets.
- Sum of bitwise AND of all possible subsets of given set
- Sum of bitwise OR of all possible subsets of given set
- Sum of average of all subsets
- How will you print numbers from 1 to 100 without using loop?
- How will you print numbers from 1 to 100 without using loop? | Set-2
- Print a pattern without using any loop
- Python | Using for loop in Flask
- Print 1 to 100 in C++, without loop and recursion
- C# | Using foreach loop in arrays
- Number of subsets with sum divisible by m

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.