top

Search

C Tutorial

.

UpGrad

C Tutorial

Convert Decimal to Binary in C

Introduction

The conversion of a decimal number (numbers between 0 – 9) into a binary number (numbers that only contain instances of 0 and 1) is possible using bitwise operators, stack and loops. Since binary values are entirely meant for the system’s comprehension, this conversion makes it simpler for humans to understand a decimal while converting it to a binary digit to enable system operation. 

Before getting into the details of how to convert decimal to binary in C, let’s look at the overview of the number system.

Getting Started with the Number System

The number system refers to a technique that represents the count or number of a given set of objects. For instance, how many fruits are in a basket? Essentially, it can be comprehended as a writing system for signifying numbers using symbols or digits. Various ways are available to denote a value.

Here are the 4 major types of number systems:

Number System

Base

Binary System

2

Octal System

8

Decimal System

10

Hexadecimal System

16

A number system’s base indicates several unique digits it uses to showcase numbers. For example, base 8 indicates eight digits, i.e., 0, 1, 2, 3, 4, 5, 6 and 7. On the other hand, base 10 indicates 10 digits (0 to 10) to display numbers.

Understanding Binary and Decimal Digits in C

Binary number system refers to a numbering technique in computer architecture which only comprises two digits- 1 and 0. The system uses base 2 to denote numbers. So, each value in a binary number can be either 0 or 1. Examples of binary numbers are 100, 101, 111, etc.

On the other hand, the decimal system utilises base 10 to denote numbers. So, each value in a decimal number system can be one of these numbers: 0,1,2,3,4,5,6,7,8,9. Examples of decimal numbers include 45, 159, etc.

Navigating Decimal and Binary Value Representation

You need to use 0 and 1 to represent binary numbers.  In order to represent decimal numbers, you need to use numbers from 0 to 9.

The following table represents decimal numbers and relevant binary number representation.

Decimal Numbers

Binary Numbers

0

0000

1

0001

2

0010

3

0011

4

0100

5

0101

6

0110

7

0111

8

1000

9

1001

10

1010

Let’s take the following example to understand how to convert a decimal number to binary.

We will take the number 7 and understand why it is represented as 0111 in binary. In its binary code, the least significant digit is 1, and its place value is 20, which is 1. Similarly, the place value designated to each digit is added to show that 0111 (with base 2) = 0 + 4 + 2 + 1 = 7 (with base 10).

Converting Decimal to Binary: Algorithm in C

The process to convert decimal to binary in C follows the below algorithm.

Step 1: Determine the remainder by dividing the input number by 2. Save the remainder within an array.

Step 2: Divide the remainder value by 2.

Step 3: Iterate these two steps till the value your receive, as a result, exceeds zero.

Step 4: Print the resulting array in a reverse format to obtain the binary form of the numeric value.

Methods of Conversion: Decimal Number to Binary Number in C

Using for-loop

To convert decimal to binary in C using for loop, we begin by allowing the user to enter any decimal number. The decimal number is stored in the variable decinum (the input number). We then use a for loop to do the conversion. The loop calculates the remainder of the decinum divided by 2. This remainder is stored in the binarynum array, and we divide the decinum by 2 to obtain the subsequent bit. This process iterates until decinum equates to zero.

After the for loop, the program iterates over the binarynum in reverse sequence and prints each bit of the binary number. Lastly, the program returns 0 to demonstrate successful implementation.

Here’s an example C program to convert decimal to binary using for loop.

#include <stdio.h>

int main()
{
int decinum, binarynum[32], x;

printf("Please enter a decimal number: ");
scanf("%d", &decinum);

// Converts decimal to binary
for (x = 0; decinum > 0; x++) {
binarynum[x] = decinum % 2;
decinum = decinum / 2;
}

printf("Binary representation is: ");
for (x = x - 1; x >= 0; x--) {
printf("%d", binarynum[x]);
}

return 0;
}

Output:

Please enter a decimal number: 5
Binary representation is: 101

Note: We have declared the binarynum array with a size of 32 because we assumed it to have a 32-bit binary representation. You can change the size as per your needs.

Using while loop

The first step in this method is to prompt the user to enter any decimal number and save it in decinum variable.

The while loop continues until decinum exceeds 0. Within the loop, we determine the remainder of decinum divided by 2. This step represents the least significant bit of the corresponding binary number.

The program then stores the remainder within the binarynum array, and divides decinum by 2 to obtain the next bit. Subsequently, it increments the index i to get the next position within the array. The process of decimal to binary in C using array continues until decinum becomes zero.

After the while loop completes, a for loop starts to iterate over the binarynum array in reverse sequence. The program prints all bits of the binary number. Lastly, the program returns 0 to show successful implementation. Note that the program for this conversion is identical to a program to convert decimal to binary in java.

Here’s the example program for decimal to binary conversion using a while loop:

#include <stdio.h>

int main() 
{
int decinum, binarynum[32], x = 0;
printf("Please enter a decimal number: ");
scanf("%d", &decinum);

// Converts decimal to binary
while (decinum > 0) {
binarynum[x] = decinum % 2;
decinum = decinum / 2;
x++;
}

printf("The binary representation of the input decimal number is: ");
for (int y = x - 1; y >= 0; y--) 
{
printf("%d", binarynum[y]);
}

return 0;
}

Output:

Please enter a decimal number: 7
The binary representation of the input decimal number is: 111

Using Stack

This method lets you define a Stack structure to denote the stack data structure. The corresponding stack holds an array of integers and an ‘up’ index to continuously track the current topmost element.

The program prompts the user to enter a decimal number and store it in decinum, while the initialize function initializes the stack.

The program then employs a while loop for binary-to-decimal conversion. The loop continually divides the number with 2 while pushing the remainder over the stack. The process continues until decinum becomes 0.

The program then uses another while loop to display the binary form. The pop function pops each bit from the stack and prints it. This loop continues until the stack goes empty ( the isStackEmpty function helps to check this).

Here’s a C program to convert binary to decimal using stack:

#include <stdio.h>
#define MAX_SIZE_LIMIT 32

typedef struct 
{
int data[MAX_SIZE_LIMIT];
int up;
} Stack;

void initialize(Stack* stack) {
stack->up = -1;
}

void push(Stack* stack, int elem) 
{
if (stack->up == MAX_SIZE_LIMIT - 1) 
{
printf("Stack Overflow\n");
return;
}
stack->up++;
stack->data[stack->up] = elem;
}

int pop(Stack* stack) 
{
if (stack->up == -1) 
{
printf("Stack Underflow\n");
return -1;
}
int elem = stack->data[stack->up];
stack->up--;
return elem;
}

int isStackEmpty(Stack* stack) {
return stack->up == -1;
}

int main() 
{
int decinum;
Stack stack;
initialize(&stack);

printf("Please enter a decimal number: ");
scanf("%d", &decinum);

// Converts decimal to binary using stack
while (decinum > 0) {
int remainder = decinum % 2;
push(&stack, remainder);
decinum = decinum / 2;
}

printf("The binary representation of the entered number is: ");
while (!isStackEmpty(&stack)) 
{
int bit = pop(&stack);
printf("%d", bit);
}

return 0;
}

Output:

Please enter a decimal number: 8
The binary representation of the entered number is: 1000

Without Using Arrays

The process to convert decimal to binary in C without an array doesn’t consume any additional room to contain the number’s binary representation. The binary form of the value is stored as a decimal number. However, be cautious of the long long integer overflow to be able to continue the process by the range of long long int.

You can implement this method using a while loop that checks until the number exceeds 0.

Using Bitwise Operators

This method checks each bit of the number to determine its binary form. The computer system stores any number as binary. So, analysing every bit helps you to determine the desired binary representation.

You can implement this method using a for loop that runs from the most significant to the least significant bit (i.e. left to right).

Conclusion

We’ve discussed various methods to convert decimal to binary in C. This conversion offers a rudimentary understanding of number representation. Moreover, it illustrates the basic concepts of binary arithmetic that streamline efficient analysis and manipulation of binary data.

Going through the tutorial is one of the most recommended approaches to initiate your C learning process. Along with this, you can pursue upGrad’s Master of Science in Computer Science offered by Liverpool John Moores University and accelerate your career in the tech industry. By pursuing this course, aspirants can learn in-demand skills and elevate their software development careers. 

Enroll now to jumpstart your journey with upGrad!

FAQs

1. Can you convert negative decimal numbers to binary using the decimal-to-binary conversion method?

Yes, you can use the mentioned approach to convert decimal numbers to binary. For that, you may have to modify the code to manipulate the sign bit and use suitable data types (for example, two's complement representation or signed integers).

2. Is there a standard library function in C to convert decimal to binary?

No standard library function in C exists to convert decimal numbers to binary. You can use library functions like itoa or sprintf for this conversion. Such functions let you form an integer in the form of a binary string. Note that these functions might not be supported in all C compilers or may demonstrate platform-specific behaviours.

3. How to convert a floating-point decimal number to binary?

The conversion of floating-point decimal numbers to binary involves isolating the fractional and integer parts of a number and then converting them separately. The corresponding fractional part conversion may undergo an iterative process. For this conversion, different techniques and algorithms are available, such as the IEEE 754 standard for signifying floating-point numbers.

Leave a Reply

Your email address will not be published. Required fields are marked *