- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Count number of binary strings without consecutive 1's in C

Given the task is to count the number of all the binary strings of length n without any consecutive 1’s.

Binary Number System is one the type of Number Representation techniques. It is most popular and used in digital systems. The binary system is used for representing binary quantities which can be represented by any device that has only two operating states or possible conditions. For example, a switch has only two states: open or close.

In the Binary System, there are only two symbols or possible digit values, i.e., 0 and 1.Represented by any device that only has 2 operating states or possible conditions. Binary strings are those strings that contain binary values i.e. either 0’s or 1’s

Let’s now understand what we have to do using an example −

**Input** − n = 2

**Output** − Count of binary strings without consecutive 1’s of 2 is : 3

**Explanation** − 00, 01, 10 so there are only 3 binary strings with length n a no consecutive 1’s

**Input** − n = 7

**Output** − Count of binary strings without consecutive 1’s of 7 is − 34

## Approach used in the below program as follows

Take an input n for string length

In count function we will count the binary strings without consecutive 1’s, define two arrays arr[] and arr_2 of size n, and a variable temp to store the result.

Assign the 0th element of both arrays as 1

Loop from i=1 till I less than n.

While in loop, Set arr[i] = arr[i-1]+arr_2[i-1] and arr_2[i] = arr[i-1]

Set temp = arr[n-1]+arr_2[n-1], and then print the temp.

## Example

#include<stdio.h> //create function to calculate binary strings without consecutive 1’s void count(int num){ int arr[num]; int arr_2[num]; int i=0, temp=0; arr[0] = arr_2[0] = 1; //loop till number isn't equals to 0 for (i = 1; i < num; i++){ arr[i] = arr[i-1] + arr_2[i-1]; arr_2[i] = arr[i-1]; } temp = arr[num-1] + arr_2[num-1]; printf("Count of binary strings without consecutive 1’s of %d is : %d",num,temp); printf("

"); } int main(){ //call the count function count(10); count(7); count(1); return 0; }

## Output

If we run the above code we will get the following output −

Count of binary strings without consecutive 1’s of 10 is : 144 Count of binary strings without consecutive 1’s of 7 is : 34 Count of binary strings without consecutive 1’s of 1 is : 2

- Related Articles
- C/C++ Program to Count number of binary strings without consecutive 1’s?
- Program to Count number of binary strings without consecutive 1’s in C/C++?
- Count Binary String without Consecutive 1's
- Python Program to Count number of binary strings without consecutive 1’
- Count number of binary strings of length N having only 0’s and 1’s in C++
- Finding maximum number of consecutive 1's in a binary array in JavaScript
- Count the number of 1’s and 0’s in a binary array using STL in C++
- C# program to check if there are K consecutive 1’s in a binary number
- C# Program to Count the Number of 1's in the Entered Numbers
- Count 1’s in a sorted binary array in C++
- Python program to check if there are K consecutive 1’s in a binary number?
- Find the number of binary strings of length N with at least 3 consecutive 1s in C++
- Count subarrays with equal number of 1’s and 0’s in C++
- Python program to find the length of the largest consecutive 1's in Binary Representation of a given string.
- Design a Moore machine to generate 1's complement of a binary number.