Published: 02 Jan 2025
Reading Time: 10 min read
Converting decimals to binary is perhaps one of those exercises that embrace computer science fundamental studies upon which the computer's way of thinking can be mastered. In day-to-day life, we deal with decimals or base 10 (0 to 9), but computers use binary numbers base 2 (0 or 1) numbers. This decimal to binary program in C conversion process is very important, especially in programming, networking and data processing.
The decimal system is the number system we use every day. It has ten digits from 0 to 9. Each digit's place in a number is important because it tells us how many tens, hundreds, or thousands it represents. For example, in 345, the 3 means 300, the 4 means 40, and the 5 means 5. All these add up to 345. This system works well for humans and day-to-day counting.
The binary system is the language of computers. It only has two digits: 0 and 1. These represent off-and-on states in electronics. In binary, each place value is a power of 2. For example, the binary number 101 means 1x 4 (or 2²) + 0 x 2 (or 2¹) + 1 x 1 (or 2⁰). When you add that up, 101 in binary equals 5 in decimal. Computers use binary because it's simple and matches their internal circuits.
The octal system uses eight digits: 0, 1, 2, 3, 4, 5, 6, and 7. Each place value in octal is a power of 8. For example, the octal number 123 means 1×64 (or 8²) + 2 x 8 (or 8¹) + 3 x 1 (or 8⁰). Adding these gives 83 in decimal. Octal is helpful in computing because it makes binary numbers shorter. Three binary digits can fit into one octal digit, making long binary numbers easier to read and write.
The hexadecimal system uses sixteen symbols: 0 to 9 and A to F. A stands for 10, B for 11, and so on up to F, which is 15. Each place value in hexadecimal is a power of 16. For example, the hexadecimal number 1A means 1 x 16 (or 16¹) + 10 x 1 (or 16⁰). When you add these, 1A in hexadecimal equals 26 in decimal. Hexadecimal is often used in computing because it makes binary numbers shorter and easier to work with. One hexadecimal digit equals four binary digits.
Decimal numbers are base-10 numbers, and these are the type we use in everyday life. In C programming, they're represented using the float or double data types. A float uses 4 bytes of memory and provides a precision of about 6-7 digits. If you need higher precision, double uses 8 bytes and offers around 15-16 digits of precision.
Binary numbers are base-2 numbers, made up of just 0s and 1s. In C, they're usually represented by the int or long int data types, which use 32 or 64 bits, respectively. To write a binary number in C, you use the prefix 0b followed by a sequence of 0s and 1s, like 0b1010.
The division method involves repeatedly dividing the decimal number by 2 and storing the remainder. These remainders represent the binary digits. The process continues until the quotient becomes 0. The binary number is then read by reversing the order of the remainders.
Using stacks helps store binary digits (remainders) as they are calculated which allows for easy reversal. The decimal number is divided by 2 in a loop, and the remainders are pushed onto the stack. After the loop ends, the stack is popped to display the binary number in the correct order.
A while loop is used to repeatedly divide the decimal number by 2, and store each remainder. The loop runs until the decimal number becomes 0. Binary digits are calculated in each iteration and either printed directly or stored for later use.
A for loop performs the same task as a while loop but with defined initialization, condition, and increment/decrement in one statement. The loop divides the number by 2 and calculates remainders to derive the binary number step by step.
Bitwise operators like & and >> can convert decimal to binary. The & 1 operation extracts the least significant bit, while >> shifts the bits to the right. This approach is efficient and avoids arithmetic operations like division.
Recursion uses a function that repeatedly calls itself with the quotient of the number divided by 2. Each remainder is calculated and printed after the recursive call returns, and the binary number is displayed in the correct order.
A user-defined function encapsulates the logic of binary conversion. For example, a function can take the decimal number as input, it then handles the conversion process internally, and return or print the binary result.
The math library can assist in binary conversion using logarithms and powers. For instance, base-2 logarithms can determine the highest bit position. It helps in manual binary representation without traditional loops or recursion.
Converting negative numbers to binary involves using two's complement representation. First, calculate the binary form of the absolute value of the number. Then, flip all the bits (1 becomes 0 and 0 becomes 1), and add 1 to the result. This method is used in computer systems to represent signed integers.
For floating-point numbers, the integer part is converted using standard methods, and the fractional part is multiplied by 2 repeatedly. The integer parts of the results are recorded to derive the binary equivalent of the fractional part.
Here's a step-by-step algorithm to convert a decimal number to binary in C:
#include <stdio.h>
int main() {
int decimal, quotient, remainder;
int binary[32]; // Array to store binary digits (supports up to 32-bit numbers)
int i;
// Prompt the user for input
printf("Enter a decimal number: ");
scanf("%d", &decimal);
quotient = decimal;
i = 0;
// Convert decimal to binary using a for loop
for (; quotient > 0; i++) {
remainder = quotient % 2;
binary[i] = remainder;
quotient = quotient / 2;
}
// Print the binary number in reverse order
printf("Binary equivalent: ");
for (int j = i - 1; j >= 0; j--) {
printf("%d", binary[j]);
}
printf("\n");
return 0;
}
Enter a decimal number: 3
Binary equivalent: 11
=== Code Execution Successful ===
#include <stdio.h>
int main() {
int decimal, quotient, remainder;
int binary[32]; // Array to store binary digits (supports up to 32-bit numbers)
int i = 0;
// Prompt the user for input
printf("Enter a decimal number: ");
scanf("%d", &decimal);
quotient = decimal;
// Convert decimal to binary using a while loop
while (quotient > 0) {
remainder = quotient % 2;
binary[i] = remainder;
quotient = quotient / 2;
i++;
}
// Print the binary number in reverse order
printf("Binary equivalent: ");
for (int j = i - 1; j >= 0; j--) {
printf("%d", binary[j]);
}
printf("\n");
return 0;
}
Enter a decimal number: 6
Binary equivalent: 110
=== Code Execution Successful ===
#include <stdio.h>
// Function to convert decimal to binary
void decimalToBinary(int decimal) {
int binary[32]; // Array to store binary digits (supports up to 32-bit numbers)
int i = 0;
// Convert decimal to binary
while (decimal > 0) {
binary[i] = decimal % 2;
decimal = decimal / 2;
i++;
}
// Print the binary number in reverse order
printf("Binary equivalent: ");
for (int j = i - 1; j >= 0; j--) {
printf("%d", binary[j]);
}
printf("\n");
}
int main() {
int decimal;
// Prompt the user for input
printf("Enter a decimal number: ");
scanf("%d", &decimal);
// Call the function to convert decimal to binary
decimalToBinary(decimal);
return 0;
}
Enter a decimal number: 8
Binary equivalent: 1000
=== Code Execution Successful ===
#include <stdio.h>
// Function to convert decimal to binary using stack (array)
void decimalToBinary(int decimal) {
int binary[32]; // Array to store binary digits (supports up to 32-bit numbers)
int top = -1; // Stack top pointer
// Convert decimal to binary using a for loop and stack
for (int quotient = decimal; quotient > 0; quotient /= 2) {
top++; // Increment stack pointer
binary[top] = quotient % 2; // Push remainder onto stack
}
// Print the binary number by popping it from the stack
printf("Binary equivalent: ");
for (int i = top; i >= 0; i--) {
printf("%d", binary[i]); // Pop and print each binary digit
}
printf("\n");
}
int main() {
int decimal;
// Prompt the user for input
printf("Enter a decimal number: ");
scanf("%d", &decimal);
// Call the function to convert decimal to binary
decimalToBinary(decimal);
return 0;
}
Enter a decimal number: 10
Binary equivalent: 1010
=== Code Execution Successful ===
#include <stdio.h>
// Function to convert decimal to binary using bitwise operator
void decimalToBinary(int decimal) {
int i;
int isLeadingZero = 1; // Flag to skip leading zeros
// Check if the decimal number is 0
if (decimal == 0) {
printf("Binary equivalent: 0\n");
return;
}
printf("Binary equivalent: ");
// Iterate through each bit using the bitwise right shift
for (i = 31; i >= 0; i--) {
int bit = (decimal >> i) & 1; // Right shift and mask to get the bit at position i
// Skip leading zeros
if (bit == 1) {
isLeadingZero = 0; // Once we find the first 1, stop skipping zeros
}
if (!isLeadingZero) {
printf("%d", bit); // Print the bit if it's not a leading zero
}
}
printf("\n");
}
int main() {
int decimal;
// Prompt the user for input
printf("Enter a decimal number: ");
scanf("%d", &decimal);
// Call the function to convert decimal to binary
decimalToBinary(decimal);
return 0;
}
Enter a decimal number: 15
Binary equivalent: 1111
=== Code Execution Successful ===
#include <stdio.h>
// Function to convert decimal to binary without using an array
void decimalToBinary(int decimal) {
// Handle the case for zero
if (decimal == 0) {
printf("Binary equivalent: 0\n");
return;
}
printf("Binary equivalent: ");
int isLeadingZero = 1; // Flag to skip leading zeros
// Iterate through each bit using the bitwise right shift
for (int i = 31; i >= 0; i--) {
int bit = (decimal >> i) & 1; // Right shift and mask to get the bit at position i
// Skip leading zeros
if (bit == 1 || !isLeadingZero) {
printf("%d", bit); // Print bit and stop skipping leading zeros
isLeadingZero = 0; // After printing the first 1, stop skipping
}
}
printf("\n");
}
int main() {
int decimal;
// Prompt the user for input
printf("Enter a decimal number: ");
scanf("%d", &decimal);
// Call the function to convert decimal to binary
decimalToBinary(decimal);
return 0;
}
Enter a decimal number: 20
Binary equivalent: 10100
=== Code Execution Successful ===
This exercise of converting decimals to binary in different ways is crucial for students to grasp the fundamentals of coding. By implementing various approaches, such as using loops, functions, bitwise operators, and arrays, students learn to think in different ways and choose the most efficient solution. It strengthens their understanding of data manipulation, logical operations, and problem-solving. Mastering these concepts lays a strong foundation for more advanced topics in programming, enhances their coding skills, and prepares them for real-world programming challenges.
Decimal to binary conversion is needed for computer science as these machines use binary (base-2) to process and store data. Understanding this conversion helps you to learn how data is represented and manipulated at the machine level.
The division by 2 methods for conversion means calculating the division of a decimal number with 2, and each digit is noted in terms of the remainder. Taking the division of each digit in a definite number from a unit digital number gives a remainder, which is 0 or 1, which forms a binary digit. The binary number is then obtained by reading the remainder from the last towards the first that was obtained during the division exercise. This process is done until the quotient is zero, and all the remainder offered in binary form signify the Decimal value of a number.
For negative numbers, you can use what is known as a two's complement to represent them in binary position. This pertains to converting the bits of the positive equivalent and making its complement by adding one more.
Using functions makes the code modular and easier to manage. It helps break down the problem into smaller, manageable parts, improving code readability and maintainability.
Bitwise operators allow direct manipulation of individual bits in a number. It is ideal for tasks like converting decimals to binary ones.
Understanding the Selection Sort Algorithm - A comprehensive guide to the Selection Sort Algorithm covering concepts, working steps, and hands-on coding examples. (04 Jan 2026, 8 min read)
Mastering Insertion Sort in C: Code, Logic, and Applications - Understand insertion sort in C with easy-to-follow logic, code examples, and practical tips. (02 Jan 2026, 6 min read)
Complete Guide on String Functions in C - Learn essential string functions in C with syntax, examples, memory rules, and safe practices. (02 Jan 2026, 5 min read)
Quick Sort Algorithm Explained: Steps, Code Examples and Use Cases - Learn the Quick Sort Algorithm with clear steps, partition logic, Python & C++ code examples. (02 Jan 2026, 6 min read)
Switch Statement in C: Syntax, Flowchart & Sample Programs - Learn how to use the switch statement in C programming with simple syntax, real-life examples, and use cases. (02 Jan 2026, 6 min read)
The Ultimate Guide to Binary Search Algorithm in C - Learn the Binary Search Algorithm with steps, examples, and efficiency insights. (02 Jan 2026, 8 min read)
Source: NxtWave CCBP
Original URL: https://www.ccbp.in/blog/articles/decimal-to-binary-program-in-c
Contact: [email protected] | +919390111761 (WhatsApp only)