**Decimal Numbers:**

Decimal numbers are the numbers in the decimal system. They are represented by the digits 0 to 9. For **example**, the number 123 is represented by the digits 1, 2, and 3. The number 0 is represented by the digit 0. The number 123456789 is represented by the digits 1, 2, 3, 4, 5, 6, 7, 8, and 9. In order to represent number system, we need a base. Decimal numbers are represented in the base 10. The base 10 is the most common base in the world.

**Binary Numbers:**

Binary numbers are the numbers in the binary system. They are represented by the digits 0 and 1. For **example**, the number 101 is represented by the digits 1 and 0. The number 0 is represented by the digit 0. The number 101010101 is represented by the digits 0, and 1. Binary numbers are represented in the base 2.

Write a C program that takes a decimal number as input and convert it to a binary number.

**Algorithm:**

The simplest way to convert a Decimal number to Binary number is to use the following algorithm:

1. Divide the number by 2 and get the quotient and remainder.

2. If the remainder is 0, then the binary number is 0.

3. If the remainder is 1, then the binary number is 1.

4. Repeat the process for the quotient and remainder.

5. For every step keep appending the remainder to the binary number.

To approach this problem, we simply use the repeated division method and take out the remainder. This program uses modulo operator to get the remainder.

There are many approaches to convert a Decimal number to Binary number. Let’s take a detailed look at all the approaches to convert a Decimal number to Binary number in C.

- Convert Decimal to Binary in C using Naive Approach
- Convert Decimal (Negative Numbers) to Binary in C
- Convert Decimal Numbers with Floating Points to Binary Number in C
- Convert Decimal to Binary in C using Recursion

This approach simply converts the decimal number to binary using the division operation and while loop.

**Methods used:**

**decimal_to_binary** – This method converts the decimal number to binary number.

**Example:**

Suppose the input is 324, the program should output 101000100.

Here is the source code of the C program to find the binary equivalent of the decimal number. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

`/*`

`* C Program to Convert Decimal to Binary number`

`*/`

`#include <stdio.h>`

long dec_to_bin(long n)

`{`

long bin = 0;

long dec = n;

long i = 1;

while (dec > 0) {

bin += (dec % 2) * i;

dec /= 2;

i *= 10;

`}`

return bin;

`}`

int main(void)

`{`

long n;

printf("Enter a decimal number: ");

scanf("%ld", &n);

printf("Binary equivalent of %ld = %ld\n", n, dec_to_bin(n));

return 0;

`}`

1. The program starts with asking the user to enter a decimal number.

2. The program then converts the decimal number to binary number via the **dec_to_bin** method.

3. Here, we have to keep in mind that our program can’t convert negative numbers to binary.

4. Therefore, for numbers which are negative, we return 0 as the binary number.

5. For positive numbers, we return the binary number after performing continued division and adding the remainders to the binary number.

6. The program prints the binary number after converting the decimal number to binary.

**Time complexity: O(n)**

The time complexity of this program is O(n) where n is the number of digits in the decimal number.

**Space Complexity: O(1)**

The space complexity of this program is O(1) since we are not using any extra space.

**Testcase 1:** In this case, we are entering the decimal number “324” as input.

Enter a decimal number: 324 Binary equivalent of 324 = 101000100

**Testcase 2:** In this case, we are entering the decimal number “-123” as input.

Enter a decimal number: -123 Binary equivalent of -123 = 0

The decimal number can be negative. For example, the number -123 is represented by the digits 1, 2, 3. The number -0 is represented by the digit 0. The number -123.456789 is represented by the digits 1, 2, 3, ., 4, 5, 6, 7, 8, and 9.

**Methods used:**

**int sizeOfNumber**– Used to find the size (number of digits) of the number and also the bitsize in which it should fit in.**long twos_complement**– Finds the twos complement of the number entered as binary.**long dec_to_bin**– Converts the decimal number to binary.**long toBinary**– Checks whether the number is negative or not and accordingly passes it to functions.

**Approach to convert decimal (neagtive number) to binary.**

1. Ask the user to enter the decimal number.

2. Check whether the number is negative or not.

3. If the number is positive, then convert it to binary.

4. If the number is negative, find it’s bitsize and convert it to two’s complement.

5. Convert the decimal number to binary.

6. Print the binary number.

**Examples:**

Suppose the input is **-123**, the program should give output as **10000101**.

Suppose the input is **100**, the program should give output as **1100100**.

Here is the source code of the C program to find the binary equivalent of the decimal negative number. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

`/* Decimal to binary numbers also taking into account negative numbers. */`

`#include <stdio.h>`

int bitsize = 8;

int sizeOfNumber(long n)

`{`

`// Repeated division by 10 to find the size of the number.`

int size = 0;

while (n > 0)

`{`

n /= 10;

`size++;`

`}`

`// Bitsize will be helpful while finding the two's complement`

if (size > 7)

bitsize = 16;

return size;

`}`

long twos_complement(long n)

`{`

long i = 1;

long twos_comp = 0;

long r;

int flag = 0;

printf("The number has %d digits so setting bitsize at %d.\n", sizeOfNumber(n), bitsize);

for (int j = 0; j < bitsize; j++)

`{`

`// Finding the remainder and appending it according to the flag.`

r = n % 10;

if (flag == 1)

twos_comp += ((r == 1) ? 0 : 1) * i;

`else`

twos_comp += r * i;

n /= 10;

i *= 10;

if (r == 1)

flag = 1;

`}`

printf("The twos complement ");

return twos_comp;

`}`

`/* Simple decimal to binary convertor */`

long dec_to_bin(long n)

`{`

long bin = 0;

long dec = n;

long i = 1;

while (dec > 0)

`{`

bin += (dec % 2) * i;

dec /= 2;

i *= 10;

`}`

return bin;

`}`

long toBinary(long n)

`{`

`// Function to check whether it's a negative or a positive number and`

`// accordingly passes through the functions.`

if (n >= 0)

return dec_to_bin(n);

`else`

return twos_complement(dec_to_bin(~n + 1));

`}`

int main(void)

`{`

long n;

printf("Enter a decimal number: ");

scanf("%ld", &n);

printf("Binary equivalent of %ld = %ld\n", n, toBinary(n));

return 0;

`}`

1. The program starts with asking the user to enter a decimal number.

2. The program then converts the decimal number to binary number via the **dec_to_bin** method.

3. In case the number is negative, we use the **twos_complement** method to find the twos complement of the number and also find the **bitsize**.

**Time complexity: O(n)**

The time complexity of this program is O(n) where n is the number of digits in the decimal number.

**Space Complexity: O(1)**

The space complexity of this program is O(1) since we are not using any extra space.

**Testcase 1:** In this case, we are entering the decimal number “-123” as input.

Enter a decimal number: -123 Binary equivalent of 324 = 10000101

**Testcase 2:** In this case, we are entering the decimal number “100” as input.

Enter a decimal number: 100 Binary equivalent of 100 = 1100100

The decimal number can have fractional points. For example, the number 123.45 is represented by the digits 1, 2, 3, ., 4, 5. The number 0.0 is represented by the digit 0. The number 123.456789 is represented by the digits 1, 2, 3, ., 4, 5, 6, 7, 8, and 9.

**Methods used:**

**long dec_to_bin**– Converts the decimal number to binary.**double fractBin**– Converts the fractional part of the decimal number to binary.**long toBinary**– Converts the decimal number to binary by separating the fractional part and integer part.

**Approach:**

1. Ask the user to enter the decimal number.

2. Separate the fractional part and integer part.

3. Send the integer part to the **dec_to_bin** method.

4. Send the fractional part to the **fractBin** method.

5. Convert the decimal number to binary.

6. Add the resulting binary numbers.

7. Print the binary number.

**Example:**

Suppose the input is **123.45**, the program should output **1111011.011100**.

Here is the source code of the C program to find the binary equivalent of the decimal number with floating points. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

`/* We will convert decimal numbers with doubleing points to binary */`

`#include <stdio.h>`

long dec_to_bin(long n)

`{`

long bin = 0;

long dec = n;

long i = 1;

while (dec > 0)

`{`

bin += (dec % 2) * i;

dec /= 2;

i *= 10;

`}`

return bin;

`}`

`// fractBin multiplies iteratively for the fractional part of the number.`

double fractBin(double n)

`{`

double bin = 0;

int threshold = 6;

double temp = n;

double j = 0.1f;

for (int i = 0; i < threshold && temp > 0; i++)

`{`

temp *= 2;

int temp_int = (int)temp;

bin += (double)temp_int * j;

temp -= (double)temp_int;

j /= 10;

`}`

return bin;

`}`

double toBinary(double n)

`{`

`// Send the part after decimal point to dec_to_bin function.`

long num_int = (long)n;

double bina = (double) dec_to_bin(num_int);

double num_dec = n - (double) num_int;

double binb = fractBin(num_dec);

return bina + binb;

`}`

int main(void)

`{`

double n;

printf("Enter a decimal number: ");

scanf("%lf", &n);

printf("Binary equivalent of %lf = %lf\n", n, toBinary(n));

return 0;

`}`

1. The program starts with asking the user to enter a decimal number.

2. The program then converts the integer part of the decimal number to binary number via the **dec_to_bin** method.

3. The fractional part of the decimal number is converted to binary number via the **fractBin** method.

4. The resulting binary numbers are added and printed.

Let’s see how this works.

So, for the integer part, it’s already clear that the binary representation can be found out simply by repeated division with 2. However in case of fractional parts, the binary equaivalent is found out by repeated multiplication by 2. Suppose the number is **12.5**, then as we’d guess the binary representation is **1100.1**. For every digit to the right of the decimal point, if an decimal gets 10 different characters, binaries gets only 2. So, that’s the reason of the multiplication.

A simple analogy would be that of a jar. If we want to tell that it’s half filled, we know that in decimal terms, we’ll say it’s 0.50 filled. So, 0.50 + 0.50 is 1.00. Similarly, in case of binary we say 0.10 as, 0.10 + 0.10 = 1.00. This simple trick could be used to understand many other aspects of binaries and their relations with decimals.

**Time complexity: O(n)**

The time complexity of this program is O(n) where n is the number of digits in the decimal number.

**Space Complexity: O(1)**

The space complexity of this program is O(1) since we are not using any extra space.

**Testcase 1:** In this case, we are entering the decimal number “123.45” as input.

Enter a decimal number: 123.45 Binary equivalent of 123.450000 = 1111011.011100

**Testcase 2:** In this case, we are entering the decimal number “3045.219” as input.

Enter a decimal number: 3045.219 Binary equivalent of 3045.219000 = 101111100101.001114

The following C program using recursion finds a binary equivalent of a decimal number entered by the user.

Here is the source code of the C program to find the binary equivalent of the decimal number using recursion. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

**Example:**

Given decimal number = 15. Formula used to convert decimal to binary is **dec % 2 + 10 * convert(dec / 2)**

- 15 % 2 that is equal to 1 + 10 * ( 15/2 ) % 2
- 7 % 2 that is equal to 1 + 10 * ( 7/2 ) % 2
- 3 % 2 that is equal to 1 + 10 * ( 3 / 2) % 2
- 1 % 2 that is equal to 1 + 10 * ( 1 / 2 ) % 2

Binary number of **15** is **1111**

/* * C Program to Convert Decimal to Binary using Recursion */ #include <stdio.h> int convert(int); int main() { int dec, bin; printf("Enter a decimal number: "); scanf("%d", &dec); bin = convert(dec); printf("The binary equivalent of %d is %d.\n", dec, bin); return 0; } int convert(int dec) { if (dec == 0) { return 0; } else { return (dec % 2 + 10 * convert(dec / 2)); } }

In this C program, we are reading a decimal number using the ‘**dec**’ variable. The function **convert()** is used to convert a number decimal system to binary system using recursion.

If else condition statement is used to check the value of ‘**dec**’ variable is equal to 0, if the condition is true execute the statement return **null** value. Otherwise if the condition is false then execute the else condition statement.

Divide the value of ‘**dec**’ variable by 2. Multiply the result value with 10 and add the value with the modulus of the value of ‘**dec**’ variable by 2. Print the Binary number of the decimal system using **printf** statement.

In this case, we are entering the decimal number “15” as input.

Enter a decimal number: 15 The binary equivalent of 15 is 1111.

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

**Next Steps:**

- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

**Related Posts:**

- Buy Computer Science Books
- Practice Computer Science MCQs
- Buy C Books
- Apply for Computer Science Internship
- Apply for C Internship