C Decimal to Binary
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:
- Divide the decimal number by 2.
- Write down the remainder.
- Divide the result from step 1 by 2.
- Write down the remainder.
- 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:
- 15 / 2 = 7 with a remainder of 1
- 7 / 2 = 3 with a remainder of 1
- 3 / 2 = 1 with a remainder of 1
- 1 / 2 = 0 with a remainder of 1
- 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.