
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Printing all subsets of {1,2,3,…n} without using array or loop in C program
Given a positive integer n we have to print all the subsets of a set of {1, 2, 3, 4,… n} without using any array or loops.
Like we have given any number say 3 s we have to print all the subset in the set {1, 2, 3} which would be {1 2 3}, {1 2}, {2 3}, {1 3}, {1}, {2}, {3} { }.
But we have to do this without using any loop or an array. So, only the recursion is the possible way to solve this type of problem without using any array or a loop.
Example
Input: 3 Output: { 1 2 3 }{ 1 2 }{ 1 3 }{ 1 }{ 2 3 }{ 2 }{ 3 }{ } Explanation: The set will be {1 2 3} from which we will find the subsets Input: 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 }{ }
Approach we will be using to solve the given problem −
- Start from num = 2^n -1 upto 0.
- Consider the binary representation of num with n number of 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 stated approach in detail how it works using a simple example −
Assuming the input n = 3, so the problem starts from num = 2^3 - 1 = 7
- Binary Representation of 7 ⇒
1 | 1 | 1 |
- Corresponing Subset ⇒
1 | 2 | 3 |
Subtracting 1 from num; num = 6
- Binary Representation of 6 ⇒
1 | 1 | 0 |
- Corresponing Subset ⇒
1 | 2 |
Subtracting 1 from num; num = 5
- Binary Representation of 5 ⇒
1 | 0 | 1 |
- Corresponing Subset ⇒
1 | 3 |
Subtracting 1 from num; num = 4
- Binary Representation of 4 ⇒
1 | 0 | 0 |
- Corresponing Subset ⇒
1 |
Likewise we will iterate until num = 0 and print all the subsets.
Algorithm
Start Step 1 → In function int subset(int bitn, int num, int num_of_bits) If bitn >= 0 If (num & (1 << bitn)) != 0 Print num_of_bits - bitn subset(bitn - 1, num, num_of_bits); Else Return 0 Return 1 Step 2 → In function int printSubSets(int num_of_bits, int num) If (num >= 0) Print "{ " Call function subset(num_of_bits - 1, num, num_of_bits) Print "}" Call function printSubSets(num_of_bits, num - 1) Else Return 0 Return 1 Step 3 → In function int main() Declare and initialize int n = 4 Call fucntionprintSubSets(n, (int) (pow(2, n)) -1) Stop
Example
#include <stdio.h> #include <math.h> // This function recursively prints the // subset corresponding to the binary // representation of num. int subset(int bitn, int num, int num_of_bits) { if (bitn >= 0) { // Print number in given subset only // if the bit corresponding to it // is set in num. if ((num & (1 << bitn)) != 0) { printf("%d ", num_of_bits - bitn); } // Check the next bit subset(bitn - 1, num, num_of_bits); } else return 0; return 1; } //function to print the subsets int printSubSets(int num_of_bits, int num) { if (num >= 0) { printf("{ "); // Printint the subsets corresponding to // the binary representation of num. subset(num_of_bits - 1, num, num_of_bits); printf("}"); // recursively calling the function to // print the next subset. printSubSets(num_of_bits, num - 1); } else return 0; return 1; } //main program int main() { int n = 4; printSubSets(n, (int) (pow(2, n)) -1); }
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 }{ }
- Related Articles
- Printing 1 to 1000 without loop or conditionals in C/C++
- C program to display all prime numbers between 1 to N using for loop
- Print a character n times without using loop, recursion or goto in C++
- Sum of the series 1^1 + 2^2 + 3^3 + ... + n^n using recursion in C++
- C++ program to find the sum of the series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n
- Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + .. + n in C++
- Program to find sum of 1 + x/2! + x^2/3! +…+x^n/(n+1)! in C++
- C++ Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! + …… n/n!
- C++ program to find the sum of the series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!
- Program to print numbers from 1 to 100 without using loop
- Prove the following by using the principle a mathematical induction for all $n\epsilon N$.$1+3+3^{2}+\ldots+3^{n-1}=\frac{\left(3^{n}-1\right)}{2}$
- Sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n) in C++
- Golang Program to check whether given positive number is power of 2 or not, without using any branching or loop
- Program to find sum of series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n in C++
- C++ program to find the sum of the series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + … + (n*n)
