# Print numbers with digits 0 and 1 only such that their sum is N

Given a number N, the task is to find the required numbers consist of only 0 and 1 digit whose sum is equal to N.

Example:

```Input: 9
Output: 1 1 1 1 1 1 1 1 1
Only numbers smaller than or equal to 9 with
digits 0 and 1 only are 0 and 1 itself.
So to get 9, we have to add 1 - 9 times.

Input: 31
Output: 11 10 10```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

1. Initialize product p to 1 and m to zero.
2. Create the vector that stores the resultant integer counts of 0s and 1s.
3. Loop for N and check if N is multiple of 10 if yes get the decimal and update p by multiplying 10 and store this value in a vector and decrease N by m do this for each decimal and print the total size of vector.
4. Finally traverse the vector and print the elements.

Below is the implementation of the above approach.

## C++

 `// C++ implementation of the above approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to count the numbers ` `int` `findNumbers(``int` `N) ` `{ ` `    ``// Initialize vector array that store ` `    ``// result. ` `    ``vector<``int``> v; ` ` `  `    ``// Get the each decimal and find its ` `    ``// count store in vector. ` `    ``while` `(N) { ` ` `  `        ``int` `n = N, m = 0, p = 1; ` `        ``while` `(n) { ` ` `  `            ``// find decimal ` `            ``if` `(n % 10) ` `              ``m += p; ` ` `  `            ``n /= 10; ` `            ``p *= 10; ` `        ``} ` ` `  `        ``v.push_back(m); ` ` `  `        ``// Decrement N by m for each decimal ` `        ``N -= m; ` `    ``} ` ` `  `    ``// Loop for each element of vector ` `    ``// And print its content. ` `    ``for` `(``int` `i = 0; i < v.size(); i++) ` `        ``cout << ``" "` `<< v[i]; ` ` `  `    ``return` `0; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `N = 31; ` `    ``findNumbers(N); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the above approach  ` `import` `java.util.*; ` ` `  `public` `class` `GfG{ ` ` `  `    ``// Function to count the numbers  ` `    ``public` `static` `int` `findNumbers(``int` `N)  ` `    ``{  ` `        ``// Initialize vector array that store  ` `        ``// result.  ` `        ``ArrayList v = ``new` `ArrayList();  ` `       `  `        ``// Get the each decimal and find its  ` `        ``// count store in vector.  ` `        ``while` `(N > ``0``) {  ` `       `  `            ``int` `n = N, m = ``0``, p = ``1``;  ` `            ``while` `(n > ``0``) {  ` `       `  `                ``// find decimal  ` `                ``if` `(n % ``10` `!= ``0``)  ` `                  ``m += p;  ` `       `  `                ``n /= ``10``;  ` `                ``p *= ``10``;  ` `            ``}  ` `       `  `            ``v.add(m);  ` `       `  `            ``// Decrement N by m for each decimal  ` `            ``N -= m;  ` `        ``}  ` `       `  `        ``// Loop for each element of vector  ` `        ``// And print its content.  ` `        ``for` `(``int` `i = ``0``; i < v.size(); i++)  ` `            ``System.out.print(``" "` `+ v.get(i));  ` `       `  `        ``return` `0``;  ` `    ``}  ` ` `  `     ``public` `static` `void` `main(String []args){ ` `         `  `        ``int` `N = ``31``;  ` `        ``findNumbers(N);  ` `     ``} ` `} ` ` `  `// This code is contributed by Rituraj Jain `

## Python3

 `# Python 3 implementation of  ` `# the above approach ` ` `  `# Function to count the numbers ` `def` `findNumbers(N) : ` ` `  `    ``# Initialize vector array that  ` `    ``# store result. ` `    ``v ``=` `[]; ` ` `  `    ``# Get the each decimal and find  ` `    ``# its count store in vector. ` `    ``while` `(N) : ` ` `  `        ``n, m, p ``=` `N, ``0``, ``1` `        ``while` `(n) : ` ` `  `            ``# find decimal ` `            ``if` `(n ``%` `10``) : ` `                ``m ``+``=` `p ` ` `  `            ``n ``/``/``=` `10` `            ``p ``*``=` `10` ` `  `        ``v.append(m); ` ` `  `        ``# Decrement N by m for ` `        ``# each decimal ` `        ``N ``-``=` `m ` ` `  `    ``# Loop for each element of vector ` `    ``# And print its content. ` `    ``for` `i ``in` `range``(``len``(v)) : ` `        ``print``(v[i], end ``=` `" "``) ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"` `: ` `     `  `    ``N ``=` `31` `    ``findNumbers(N) ` ` `  `# This code is contributed by Ryuga `

## C#

 `// C# implementation of the above approach  ` `using` `System; ` `using` `System.Collections; ` ` `  `class` `GfG ` `{  ` ` `  `    ``// Function to count the numbers  ` `    ``public` `static` `int` `findNumbers(``int` `N)  ` `    ``{  ` `        ``// Initialize vector array that store  ` `        ``// result.  ` `        ``ArrayList v = ``new` `ArrayList();  ` `         `  `        ``// Get the each decimal and find its  ` `        ``// count store in vector.  ` `        ``while` `(N > 0)  ` `        ``{  ` `            ``int` `n = N, m = 0, p = 1;  ` `            ``while` `(n > 0) ` `            ``{  ` `         `  `                ``// find decimal  ` `                ``if` `(n % 10 != 0)  ` `                    ``m += p;  ` `                     `  `                ``n /= 10;  ` `                ``p *= 10;  ` `            ``}  ` `            ``v.Add(m);  ` `         `  `            ``// Decrement N by m for each decimal  ` `            ``N -= m;  ` `        ``}  ` `         `  `        ``// Loop for each element of vector  ` `        ``// And print its content.  ` `        ``for` `(``int` `i = 0; i < v.Count; i++)  ` `            ``Console.Write(``" "` `+ v[i]);  ` `         `  `        ``return` `0;  ` `    ``}  ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{  ` `        ``int` `N = 31;  ` `        ``findNumbers(N);  ` `    ``}  ` `}  ` ` `  `// This code is contributed by PrinciRaj1992  `

## PHP

 ` `

Output:

```11 10 10
```

My Personal Notes arrow_drop_up

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.