×

Decimal to Binary in C

What is a decimal number?

A decimal number is a number represented in the decimal number system. This system of binary conversion uses base 10 to represent numbers, i.e. the digits in the list is one of the following numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 0.

For Example: 12, 132, 45 etc.

What is a binary number?

A binary number is a number that is being represented in the binary number system. This system uses the base as 2 to represent numbers, i.e. each digit in a binary number is one of the following numbers: 0 or 1.

For Example: 1010, 1110, 1001 etc.

To convert a decimal number to binary, we should follow the following steps:

  1. Divide the decimal number by 2.
  2. Write down the remainder.
  3. Divide the result from step 1 by 2.
  4. Write down the remainder.
  5. Repeat this process until the result of the division is 0.

For example, to convert the decimal number 15 to binary, we have followed the above steps like this:

  1. 15 / 2 = 7 with a remainder of 1
  2. 7 / 2 = 3 with a remainder of 1
  3. 3 / 2 = 1 with a remainder of 1
  4. 1 / 2 = 0 with a remainder of 1
  5. So, the binary representation of 15 is 1111.

Here's an example in C that demonstrates how to implement this algorithm:

Example 1:

// convert decimal to binary


#include <stdio.h>
#include <math.h>


long long convert(int);


int main() {
  int n, bin;
  printf("Enter a decimal number: ");
  scanf("%d", &n);
  bin = convert(n);
  printf("%d in decimal =  %lld in binary", n, bin);
  return 0;
}


long long convert(int n) {
  long long bin = 0;
  int rem, i = 1;


  while (n!=0) {
    rem = n % 2;
    n /= 2;
    bin += rem * i;
    i *= 10;
  }


  return bin;
}

Output:

Enter a decimal number: 15
15 in decimal =  1111 in binary

Here’s another program explaining the same:

Example 2:

#include <stdio.h>
#include <math.h>
int main()


{


int num, i = 0, j;
int binary[100];


printf("Enter a decimal number: ");
scanf("%d", &num);


while (num> 0)
    {
binary[i] = num % 2;
num = num / 2;
i++;
    }


printf("Binary equivalent: ");


for (j = i - 1; j >= 0; j--)
printf("%d", binary[j]);


return 0;
}

Output:


Enter a decimal number: 25
Binary equivalent: 11001

Program Explanation:

Here, we can see that this example takes a decimal number as an argument and prints out its binary representation.

Decimal to binary using loop in C

Example 3:

#include <stdio.h>


void decToBinary(int n) 
{ 
int binaryNum[32]; 
int i = 0; 
while (n > 0) { 
binaryNum[i] = n % 2; 
        n = n / 2; 
i++; 
    } 
for (int j = i - 1; j >= 0; j--) 
printf("%d", binaryNum[j]); 
} 


int main() 


{ 
int n;
printf("Enter a decimal number: ");
scanf("%d", &n);
printf("Binary equivalent of %d is: ", n);
decToBinary(n);
return 0; 


}

Output:

Enter a decimal number: 25
Binary equivalent of 25 is: 11001

Program Explanation:

This program first defines a function decToBinary that takes an integer n as input and converts it to binary using a loop. It stores the remainder when n is divided by 2 in an array called binaryNum until n becomes 0. Then, it prints the array in reverse order to get the binary equivalent of the decimal number.

In the main function, the program prompts the user to enter a decimal number, reads it using scanf, and then calls the decToBinary function to convert it to binary and print the result.

Using Bitwise Operators:

Example 4:

#include <stdio.h>
void decToBinary(int n) 
{ 
for (int i = 10; i>= 0; i--) { 
int k = n >>i; 
if (k & 1) 
printf("1"); 
else
printf("0"); 
    } 
} 


int main() 
{ 
int n;
printf("Enter a decimal number: ");
scanf("%d", &n);
printf("Binary equivalent of %d is: ", n);
decToBinary(n);
return 0; 
}

Output:

Enter a decimal number: 25
Binary equivalent of 25 is: 00000011001

Program Explanation:

This program first defines a function decToBinary that takes an integer n as input and converts it to binary using bitwise operators. It uses a loop to iterate over the bits of n from the most significant bit (MSB) to the least significant bit (LSB). For each bit, it shifts n to the right by i bits and then ANDs it with 1 to check if the bit is set. If the bit is set, it prints 1, otherwise it prints 0.

In the main function, the program prompts the user to enter a decimal number, reads it using scanf, and then calls the decToBinary function to convert it to binary and print the result.


Related Topics

Format Specifier in C

Format Specifier in C Format specifiers can be described as an operator that is used to print the data referred by any object or variable in combination with the printf ()...

3 minutes read.

Loop Questions in C

Question 1: What is For loop syntax? Ans: The syntax that has been used for the ‘for’ loop in C programming language is: for (initialization statement /*for providing a value to variables*/; test...

19 minutes read.

Bubble sort in C

Bubble sort in C Bubble sort is defined as the algorithm that is used for the sorting mechanism. It follows the technique of replacing the first index with the more minor...

4 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it...

3 minutes read.

Compound Interest Program in C

Interest on loans and deposits is compound interest. This is the most commonly used concept in everyday life. Compound interest on an amount is based on principal and interest earned...

3 minutes read.

Prime Number code in C

Prime Number Programs in C language In this tutorial, we will learn how to check whether the given number is a prime number or not, and how to print all the...

4 minutes read.

Floor() Function in C

Floor() function is a built-in function in C which is defined in the math.h header file. This function is used to return the nearest integer value which is less than...

4 minutes read.

Call Back Function in Embedded C

Callback function are one the most remarkable systems in C. A callback function is any code that is passed as a contention to another code, so this is the last...

3 minutes read.

Return array from function in C

Return array from function in C C programming does not require the return to a function of a whole array as an argument. However, you can return a pointer to an array without...

2 minutes read.

How to use floor() function in C

Introduction: The floor() used as a function in the C programming language. This function uses to return the largest or most significant integer value. The integer value is smaller than...

3 minutes read.

Different ways to Declare the Variable as Constant in C

There are a wide range of ways of making the variable as steady Using const keyword: The const catchphrase permits a software engineer to inform the compiler that a specific variable should...

5 minutes read.

Character Set in C

Introduction Every language has some basic elements which are used to represent information. The English language includes alphabets which together shape a sentence, after which the sentences are used to shape...

3 minutes read.

File Handling in C

File Handling is used to open, read, write, search and close file.  C files I/O functions handles data on secondary storage device. File handling function: There are varioushandling functions in C. Function Operation fopen() It is...

4 minutes read.

Merge and Merge sort with example in C

Assume we have two ordered Integer arrays, a[] and b[]. If we wish to combine them into another ordered array, such as c[], we can use the following straightforward technique. Compare...

3 minutes read.

Multiplication table program in C using For loop

Before we move on the program of multiplication table of any natural number, first we have to know about the For loop statement. The syntax of ‘for’ loop in C programming...

3 minutes read.

Union Program in C

 How should a union be defined?  In C programming, a Union is a method used to organize different data types into groups in the same way that Structures are used. We...

4 minutes read.

Structure in C

Why Structure came into the picture? In the actual world, we deal with entities that are collections of objects rather than tiny bits of data like integers, characters, floats, etc. So,...

4 minutes read.

How to include graphics.h in C?

How to include graphics.h in code blocks? Graphics .h is a header that allows drawing lines, rectangles, ovals, arcs, polygons, pictures, and strings on a graphical window by giving access to...

6 minutes read.

#error #pragma in C

#error, #pragma in C #error, also known as the error directive in the C language. It will not allow you to make any compilation fail and immediately issues a statement which...

4 minutes read.

gets() function in C

gets() is a pre-define or built-in function present in the stdio.h header file. stdio stands for standard input and output. gets() function is used to read a stream and store...

4 minutes read.