×

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

Long int in C

We use data type to avoid the type of data to be stored for a variable at the time of declaration.So, we can say that the variable type is known...

3 minutes read.

Distance Vector Routing Protocol Program in c

A distance-vector routing protocol is one of the foremost instructions of routing protocols in pc conversation principle for packet-switched networks. The hyperlink-nation protocol is the alternative foremost class.The Bellman-Ford set...

4 minutes read.

Command line arguments in C

Command line arguments in C The arguments that are generally passed from the line of command are referred to as command line arguments. These command line arguments are always handled by...

3 minutes read.

Ceil and Floor in C

In arithmetic, a rational number is a number that can be expressed as the quotient p/q of two integers. Where q is zero. The set of rational numbers includes all...

6 minutes read.

C Program to Find Largest Number Using Dynamic Memory Allocation

This tutorial will teach us how to locate the highest number a user has ever input into a dynamically allocated memory. C Program: #include <stdio.h> #include <stdlib.h> int main ()  {   int no;   double *data_n;   printf...

1 minute read.

Two-Dimensional array in C

What is an Array? The collection of a fixed number of elements of homogeneous data types that are stored in contiguous locations in the memory is known as an array. Only...

6 minutes read.

Branching Statements in C

What is branching in c? Branching gets its name from the fact that the computer can select which branch to follow.Programs written in the C language execute statements one after the...

6 minutes read.

Do WHILE LOOP in C Programming Examples

Before understanding the programming examples of the Do-While loop, we have to know what is Do-While loop in C. So, let's start with the definition of the Do-While loop. What is...

5 minutes read.

Write() function in c

As the name suggests the write () function is used to write the file descriptor. In other words it is used to write any file name without specifying file name,...

3 minutes read.

Run Time Polymorphism in C

What is polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. An individual who can have...

4 minutes read.

10 Best IDEs for C or C++ Developers in 2024

Nobody can deny the fact that C and C++ were the first programming languages used by significant developers worldwide. Even now, newcomers who want to start programming are most frequently...

6 minutes read.

Modulus on Negative Numbers in C

In this tutorial, we will explore some examples of modulus on negative number. What is Modulus of Negative number? By omitting the minus sign, one can determine the modulus of a negative...

3 minutes read.

Find occurrence of substring in C using function

Introduction: In this article, we discuss finding the occurrence of a substring in C using function. This software takes strings and substrings as input and counts the occurrences of substrings within...

3 minutes read.

Random Access - Lseek in C

Introduction lseek is a system call that is used to alter the location of the read/write pointer of a file descriptor. The lseek system call basically moves the current read/write location...

4 minutes read.

C Program to find the Roots of a Quadratic Equation

What is Quadratic Equation: Equations of degree 2 in polynomial form are called quadratic equations. A, B, and C are the coefficient variables in the equation, which is written as ax2...

3 minutes read.

Purpose of a Function Prototype in C

A function prototype in C is a declaration of a function that specifies the function's name, return type and parameters. It has the following syntax: return_type function_name(parameter_list); For example, the prototype for...

4 minutes read.

Int in C

The keyword int in C programming stands for integer and it is a data type which is used for variable declarations or declaration of functions of different types. Similar to...

4 minutes read.

Evaluation of Arithmetic Expression in C

We can use the "eval" function in C to evaluate an arithmetic expression stored as a string. However, using "eval" is generally considered bad practice and can lead to security...

4 minutes read.

C Programming Tutorial

What is C Programming C is most popular and widely used computer programming language. It was developed by Dennis Ritchie in 1972 at the Bell Lab. It is used to develop system application software. There...

8 minutes read.

LCM of two numbers in C

LCM is a mathematical term which stands for Least Common Multiple. LCM of any two numbers is the smallest positive value(number) which is evenly divisible by the two given numbers. Consider...

4 minutes read.