DATA STRUCTURES PROGRAM • QUEUES & DEQUES

Generate Binary Numbers Using a Queue

Learn how to generate binary numbers using a queue using a clear C program.

BeginnerQueueFIFOfront/rear

PROBLEM UNDERSTANDING

Input and expected output

Sample input
No input required
Sample output
1 10 11 100 101 110 111 1000

COMPLETE C PROGRAM

Complete C implementation

dsa-generate-binary-numbers-queue.c
Open in compiler
#include <stdio.h>

int main(void)
{
    unsigned int queue[20];
    int front = 0, rear = 0;
    queue[rear++] = 1;
    for (int count = 0; count < 8; count++) {
        unsigned int current = queue[front++];
        printf("%u ", current);
        queue[rear++] = current * 10;
        queue[rear++] = current * 10 + 1;
    }
    putchar('\n');
    return 0;
}

GUIDED CODE TOUR • NOT LIVE EXECUTION

Study the program line by line

Use the real compiler button above to run and debug with different inputs.

CURRENT STEP

Select Start to walk through the important lines.

SELECTED LINE

No line selected

EXPECTED OUTPUT FOR THE SAMPLE

1 10 11 100 101 110 111 1000
0%Step 0 of 0

PROGRAM EXPLANATION

Algorithm and explanation

  1. Read the required input values.
  2. For each dequeued binary string, enqueue copies with zero and one appended.
  3. Display the computed result.

For each dequeued binary string, enqueue copies with zero and one appended.

EFFICIENCY

Time and space complexity

Time complexity

O(n)

Auxiliary space

O(n)

DEBUGGING CHECKLIST

Common mistakes

Check this

Use the correct format specifier for every variable.

Check this

Initialize variables before using their values.

Check this

Check braces, semicolons and input order carefully.

Try it yourself

Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.