Decimal to Binary Conversion in C. Decimal to Binary Conversion is a fundamental concept in computer science and programming. Decimal to binary conversion plays a very crucial role in various applications such as encoding, cryptography, and digital systems. In this article, we will write a C program that performs decimal to binary conversion.

## Table of Contents

## Binary and Decimal Number System

Binary numbers represent values using powers of 2, where each digit is a power of 2 starting from the rightmost digit. while the decimal number system known as the base 10 system, is the standard system for integer and noninteger numbers. The decimal number system uses 10 digits- 0,1,2,3,4,5,6,7,8 and 9 to represent numerical values. In the decimal system, each digit position in a number is a power of 10.

## Decimal to Binary Conversion Process

The Decimal to Binary conversion process involves dividing the decimal number by 2 and noting the remainder at each step. The remainders, read in reverse order form the binary equivalent of the decimal number.

## C Program for Decimal to Binary Conversion

```
#include<stdio.h>
void decimalToBinary(int n){
int binaryNumber[32];
int i = 0; // Counter for binaryNumber array
if(n==0){
printf("Binary equivalent: 0");
return ;
}
while (n>0)
{
binaryNumber[i] = n%2;//storing the reminder in binary array
n = n/2;
i++;
}
// printing binary equivalent in reverse order
printf("Binary equivalent: ");
for(int j=i-1;j>=0;j--){
printf("%d",binaryNumber[j]);
}
}
void main(){
int decimalNumber;
printf("Enter the decimal number: ");
scanf("%d", &decimalNumber);
decimalToBinary(decimalNumber);
}
```

### Output

## Decimal to Binary Conversion Code Explanation

- The code starts with including standard input output library stdio.h.
- A function is declared with anme decimal to binary. This function takes an integer as an argument for decimal to binary conversion.
- Inside function an arrray name binaryNumber is declared of size 32 to store binary digits.
- Inside function counter i is used to keep track of index in binaryNumber array.
- Inside the while loop, the function calculates the remainder when decimal number is divided by 2 and stores it in the binary number array.Then it updates the decimal number by dividing it by 2.
- The while loop continues until deciaml number becomes 0.
- After the while loop the function prints the binary equivalent by itterating through binaryNumber array in reverse order.
- Inside main function program ask user to enter the number.
- Main function calls deciamlToBinary function, passing the entered decimal number as an argument.

*Happy Coding*

## 1 thought on “Decimal to Binary Conversion in C”