Print N | C ++ program to print all N - bit binary ; function to generate n digit numbers ; if number generated ; Append 1 at the current number and reduce the remaining places by one ; If more ones than zeros , append 0 to the current number and reduce the remaining places by one ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printRec ( string number , int extraOnes , int remainingPlaces ) { if ( 0 == remainingPlaces ) { cout << number << " ▁ " ; return ; } printRec ( number + "1" , extraOnes + 1 , remainingPlaces - 1 ) ; if ( 0 < extraOnes ) printRec ( number + "0" , extraOnes - 1 , remainingPlaces - 1 ) ; } void printNums ( int n ) { string str = " " ; printRec ( str , 0 , n ) ; } int main ( ) { int n = 4 ; printNums ( n ) ; return 0 ; }