C PROGRAMMING PROGRAM • LOOPS & NUMBER PROBLEMS
Convert Decimal to Binary
Learn how to convert decimal to binary using a clear C program.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
45
Sample output
Binary = 101101
COMPLETE C PROGRAM
Complete C implementation
#include <stdio.h>
int main(void)
{
unsigned int number, value;
int bits[32], count = 0;
printf("Enter a non-negative decimal integer: ");
scanf("%u", &number);
value = number;
do {
bits[count++] = (int) (value % 2);
value /= 2;
} while (value > 0);
printf("Binary = ");
for (int index = count - 1; index >= 0; index--) printf("%d", bits[index]);
printf("\n");
return 0;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
Binary = 101101
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Collect remainders from repeated division by two and print them in reverse order.
- Display the computed result.
Collect remainders from repeated division by two and print them in reverse order.
EFFICIENCY
Time and space complexity
Time complexity
O(log n)
Auxiliary space
O(log 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.
