
- 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
Print numbers with digits 0 and 1 only such that their sum is N in C Program.
Given an integer n, the task is to print the numbers who consist only 0’s and 1’s and their sum is equal to the integer n.
The numbers which contain only zeros and ones are 1, 10, 11 so we have to print all those numbers which can be added to form the sum equal to n.
Like, we entered n = 31 then the answer can be 10+10+11 or 10+10+10+1 the
Example
Input: 31 Output:10 10 10 1
Algorithm
int findNumbers(int n) START STEP 1: DECLARE AND ASSIGN VARAIBALES m = n % 10, a = n STEP 2: LOOP WHILE a>0 IF a/10 > 0 && a > 20 THEN, SUBTARCT 10 FROM a AND STORE BACK IT IN a PRINT "10 " ELSE IF a-11 == 0 THEN, SUBTRACT 11 FROM a AND STORE BACK IN a PRINT "11 " ELSE PRINT "1 " DECREMENT a BY 1 END IF END LOOP STOP
Example
#include <stdio.h> // Function to count the numbers int findNumbers(int n){ int m = n % 10, a = n; while(a>0){ if( a/10 > 0 && a > 20 ){ a = a-10; printf("10 "); } else if(a-11 == 0 ){ a = a-11; printf("11 "); } else{ printf("1 "); a--; } } } // Driver code int main(){ int N = 35; findNumbers(N); return 0; }
Output
If we run the above program then it will generate the following output:
10 10 1 1 1 1 11
- Related Articles
- Print n numbers such that their sum is a perfect square
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1 in C++
- Recursive program to print all numbers less than N which consist of digits 1 or 3 only in C++
- Print all odd numbers and their sum from 1 to n in PL/SQL
- Print all n-digit numbers whose sum of digits equals to given sum in C++
- Print all the combinations of N elements by changing sign such that their sum is divisible by M in C++
- Maximum sum of distinct numbers such that LCM of these numbers is N in C++
- Print first k digits of 1/n where n is a positive integer in C Program
- Count ordered pairs of positive numbers such that their sum is S and XOR is K in C++
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M in C++
- Find a number x such that sum of x and its digits is equal to given n in C++
- Find the number of integers from 1 to n which contains digits 0’s and 1’s only in C++
- Print prime numbers with prime sum of digits in an array
- Count pairs of numbers from 1 to N with Product divisible by their Sum in C++
- Compute sum of digits in all numbers from 1 to n

Advertisements