Do you think IIT Guwahati certified course can help you in your career?
No
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.
In C, binary values represent numbers using only 0 and 1 (base-2). They are often used for low-level bit manipulation and logic operations.
What is Decimal in C?
Decimal numbers in C are represented in base-10 (digits 0-9). They are the default numeric representation used for arithmetic and display.
Decimal and Binary Value Representation
Decimal and binary systems represent numbers differently. Decimal uses base-10 (0-9), while binary uses base-2 (0 and 1). Conversions are essential in programming and computing.
Decimal Numbers
Binary Numbers
0
0000
1
0001
2
0010
3
0011
4
0100
5
0101
6
0110
7
0111
8
1000
9
1001
10
1010
Algorithm to Convert Decimal to Binary in C
Converting a decimal (base-10) number to binary (base-2) involves repeatedly dividing the number by 2 and storing the remainders. These remainders, when read in reverse order, represent the binary equivalent of the decimal number.
Input the decimal number. Take the decimal number (let's call it n).
Divide by 2 repeatedly. Divide n by 2, and store the remainder (n % 2).
Store the remainders. The remainders are collected (starting from the least significant bit), as these represent the binary digits.
Repeat the division until the quotient becomes 0.
Reverse the order of the remainders to form the binary number, as the first remainder corresponds to the least significant bit.
Output the binary number.
Methods of Conversion: Decimal to Binary in C
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.
Implementation in C
C
C
// 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; }
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.
Implementation in C
C
C
// 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; }
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.
Implementation in C
C
C
// 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; }
Enter a decimal number: 13
Binary Representation: 1101
Method 5 (Using a While Loop)
This method is similar to the first one, but we use a while loop instead of a for loop to repeatedly divide the decimal number by 2 and store the remainders.
C
C
#include <stdio.h>
void decimalToBinaryWhileLoop(int n) { int binary[32]; // Array to store binary digits int i = 0;
// Edge case for 0 if (n == 0) { printf("Binary Representation: 0\n"); return; }
// Convert decimal to binary using a while-loop while (n > 0) { binary[i] = n % 2; // Store remainder n = n / 2; // Divide by 2 i++; }
// Print binary in reverse order printf("Binary Representation: "); for (int j = i - 1; j >= 0; j--) { printf("%d", binary[j]); } printf("\n"); }
int main() { int decimalNumber;
// Input a decimal number printf("Enter a decimal number: "); scanf("%d", &decimalNumber);
// Function call to convert decimal to binary using while-loop decimalToBinaryWhileLoop(decimalNumber);
// Stack to hold binary digits int stack[32]; int top = -1;
// Convert decimal to binary using stack while (n > 0) { stack[++top] = n % 2; // Push remainder onto stack n = n / 2; // Divide by 2 }
// Print binary by popping from the stack printf("Binary Representation: "); while (top >= 0) { printf("%d", stack[top--]); // Pop and print } printf("\n"); }
int main() { int decimalNumber;
// Input a decimal number printf("Enter a decimal number: "); scanf("%d", &decimalNumber);
// Function call to convert decimal to binary using stack decimalToBinaryStack(decimalNumber);
Enter a decimal number: 25
Binary Representation: 11001
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.
How to convert decimal to binary?
To convert decimal to binary, repeatedly divide the decimal number by 2, storing the remainders. Read the remainders in reverse order for the binary result.
Conclusion
In conclusion, converting a decimal number to binary in C involves using arrays, storing remainders, and constructing the binary representation. This process can be implemented through loops or recursion, and it's a fundamental technique for understanding data representation and bitwise operations.