Decimal to Binary in C
Introduction
The number system is the technique to represent and work with numbers. The four main types of number systems are:
- Binary Number System (base 2)
- Octal Number System (base 8)
- Decimal Number System (base 10)
- Hexadecimal Number System (base 16)
It is possible to convert any decimal number (base-10 (0 to 9)) into a binary number(base-2 (0 or 1)) with the help of a C program. A decimal number is a base 10 number because it ranges from 0 to 9; there are 10 digits between 0 to 9. Any combination of digits is a decimal number, such as 23, 445, 132, 0, 2, etc. A binary number has a base 2 as it is either 1 or 0. Any combination of 1 and 0 is a binary number, such as 1001, 101, 11111, 101010, etc.
Problem Statement
Given a decimal number as input, write a program to convert the given decimal number into an equivalent binary number.
Example 1
Input: 10
Output: 1010
Example 2
Input: 8
Output: 1000
Example 3
Input: 33
Output: 100001
Method 1 (Using Arrays)
For Example:
If the decimal number is 9.
Step 1: When 9 is divided by 2, the remainder is one. Therefore, arr[0] = 1.
Step 2: Divide 9 by 2. New number is 10/2 = 4.
Step 3: We get remainder 0 when 4 is divided by 2. Therefore, arr[1] = 0.
Step 4: Divide 4 by 2. New number is 4/2 = 2.
Step 5: We get remainder as zero when 2 is divided by 2. So, arr[2] = 0.
Step 6: Divide 2 by 2. Hence, new number is 2/2 = 1.
Step 7: The remainder when 1 is divided by 2 is 1. So, arr[3] = 1.
Step 8: Divide 1 by 2. New number is 1/2 = 0.
Step 9: Since number becomes = 0, we now print the array in reverse order. At last the equivalent binary number is 1001.
The C code for the above logic is:
// convert decimal to binary
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10],n,i;
printf("Enter the number to convert: ");
scanf("%d",&n);
printf("%d",n);
for(i=0;n>0;i++)
{
a[i]=n%2;
n=n/2;
}
printf("\nBinary of Given Number is= ");
for(i=i-1;i>=0;i--)
{
printf("%d",a[i]);
}
return 0;
}
Output:
Enter the number to convert: 5
Binary of Given Number is= 101
You can also read about dynamic array in c.
Method 2 (Without using arrays)
- Firstly, we run a while loop until a number is greater than 0.
- Then we, divide the number by 2 and find the remainder, then store the remainder in an array.
- After this, divide the number by 2.
- We shall repeat the above two steps until the number is greater than zero.
- At last, print the array in reverse order to get the binary representation of the number.
The C code for the above logic is:
// 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: 11 in decimal = 1011 in binary
Method 3 (Using Bitwise Operators)
The idea used here in this algorithm is to find a binary representation of the number by reviewing every bit of the number. As we know that any number is stored as binary in the computer system, we can find the needed binary representation by checking every bit.
The steps for this method are
- Run a for loop from the most significant bit to the least significant bit (i.e., from left to right in the number).
- We will then check whether the bit is 1 or 0. We can do it by taking 'bitwise and' of mask and num; if it is zero, the current bit is 0; else, it is 1.
- Print the bit after every iteration to get a binary representation of the number.
The C code for the above logic is:
// convert decimal to binary
#include <stdio.h>
#include <math.h>
// this method will print all the 32 bits of a number
void decimalToBinary(int num) {
for (int i = 31; i >= 0; i--) {
//calculate bitmask to check whether ith bit of num is set or not
int mask = (1 << i);
// ith bit of num is set
if (num & mask)
printf("1");
// ith bit of num is not set
else
printf("0");
}
}
int main() {
int num = 7;
decimalToBinary(num);
return 0;
}
Output:
00000000000000000000000000000111
you can implement code with the help of online c compiler for good practice.
Must read decimal to binary c++
Frequently Asked Questions
What are the Rules for Converting Decimal to Binary?
The basic rules are:
- We initially write down the number.
- Then divide it by 2 and note the remainder.
- After this, we divide the quotient obtained by 2 and record the remainder.
- We will then repeat the same process till we get 0 as the quotient.
- At last, write the values of all the remainders starting from the bottom to the top.
Can you use binary in C?
Binary literals don't exist in C. The closest we have are hexadecimal, as they closely follow the binary bit pattern. Hex to binary is easily convertible.
Can float have decimals?
While doing math with floats, we need to add a decimal point. If not used, then it will be treated as an int. The float data type has only 6 to 7 decimal digits of precision. This means the total number of digits, not the number to the right of the decimal point.
What is double in C?
A double type variable is a 64-bit floating data type. C, C++, C#, and many other programming languages identify the double as a type. A double type can denote fractional as well as whole values. It can contain 15 digits before and after the decimal point.
Conclusion
This article demonstrated various methods for converting a decimal number to a binary number in C.
Once you are done with this, you may check out our Interview Preparation Course to level up your programming journey and get placed at your dream company.
Refer to our guided paths on Codestudio to learn more about DSA, Competitive Programming, System Design, JavaScript, etc. Enroll in our courses, refer to the mock test and problems available, interview puzzles, and look at the interview bundle and interview experiences for placement preparations.
We hope that this blog has helped you increase your knowledge and if you liked this blog, check other links. Do upvote our blog to help other ninjas grow. Happy Coding!"