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=i1;i>=0;i)
{
printf("%d",a[i]);
}
return 0;
}
You can also try this code with Online C Compiler
Run Code
Output:
Enter the number to convert: 5
Binary of Given Number is= 101
You can also try this code with Online C Compiler
Run Code
You can also read about dynamic array in c, and Tribonacci Series
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;
}
You can also try this code with Online C Compiler
Run Code
Output:
Enter a decimal number: 11 in decimal = 1011 in binary
You can also try this code with Online C Compiler
Run Code
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;
}
You can also try this code with Online C Compiler
Run Code
Output:
00000000000000000000000000000111
You can also try this code with Online C Compiler
Run Code
you can implement code with the help of online c compiler for good practice.
Must read decimal to binary c++ and Short int in C Programming
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 64bit 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.
Also read reverse a number.
Refer to our guided paths on Coding Ninjas Studio 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!"