# C Program to convert Decimal to Binary

Certainly! Here’s an example of a C program that converts a decimal number to binary:

```#include <stdio.h>

void decimalToBinary(int decimal) {
int binary[32];
int i = 0;

// Handle the case when the decimal number is 0
if (decimal == 0) {
printf("Binary: 0\n");
return;
}

// Convert decimal to binary
while (decimal > 0) {
binary[i] = decimal % 2;
decimal = decimal / 2;
i++;
}

// Print the binary representation
printf("Binary: ");
for (int j = i - 1; j >= 0; j--) {
printf("%d", binary[j]);
}
printf("\n");
}

int main() {
int decimal;

printf("Enter a decimal number: ");
scanf("%d", &decimal);

decimalToBinary(decimal);

return 0;
}
```

This program takes a decimal number as input and converts it to binary using the repeated division-by-2 method. The binary digits are stored in an array and then printed in reverse order to obtain the correct binary representation.

You can run this program and provide a decimal number to get its binary equivalent.

### Decimal Number:

If you would like to convert a specific decimal number to binary, you can modify the program and provide the desired decimal number in the `main` function. For example:

```#include <stdio.h>

void decimalToBinary(int decimal) {
int binary[32];
int i = 0;

// Handle the case when the decimal number is 0
if (decimal == 0) {
printf("Binary: 0\n");
return;
}

// Convert decimal to binary
while (decimal > 0) {
binary[i] = decimal % 2;
decimal = decimal / 2;
i++;
}

// Print the binary representation
printf("Binary: ");
for (int j = i - 1; j >= 0; j--) {
printf("%d", binary[j]);
}
printf("\n");
}

int main() {
int decimal = 42; // Change this line to the desired decimal number

decimalToBinary(decimal);

return 0;
}
```

In this modified program, the decimal number is set to `42` by default. You can change the value of the `decimal` variable in the `main` function to convert any other decimal number to binary.

### Binary Number:

If you would like to convert a specific binary number to decimal, you can modify the program and provide the desired binary number in the `main` function. Here’s an example:

```#include <stdio.h>
#include <math.h>

int binaryToDecimal(long long binary) {
int decimal = 0, i = 0, remainder;

// Convert binary to decimal
while (binary != 0) {
remainder = binary % 10;
binary = binary / 10;
decimal += remainder * pow(2, i);
++i;
}

return decimal;
}

int main() {
long long binary = 101010; // Change this line to the desired binary number

int decimal = binaryToDecimal(binary);

printf("Decimal: %d\n", decimal);

return 0;
}
```

In this modified program, the binary number is set to `101010` by default. You can change the value of the `binary` variable in the `main` function to convert any other binary number to decimal.

### Decimal to Binary Conversion Algorithm:

The algorithm to convert a decimal number to binary is relatively simple. Here’s a step-by-step explanation of the algorithm:

1. Take the decimal number as input.
2. Create an empty container to hold the binary digits.
3. Check if the decimal number is 0. If it is, then the binary representation is also 0.
4. While the decimal number is greater than 0, perform the following steps: a. Take the remainder of the decimal number when divided by 2. This will be the least significant bit (LSB) of the binary representation. b. Append the LSB to the container. c. Divide the decimal number by 2, discarding any remainder.
5. Reverse the order of the binary digits in the container.
6. The resulting binary representation is the contents of the container.

Here’s a summarized version of the algorithm:

```Input: decimal number
Output: binary representation

1. Initialize an empty container for binary digits.
2. If decimal number is 0, return 0.
3. While decimal number > 0, do:
a. Get the remainder of decimal number divided by 2.
b. Append the remainder to the container.
c. Divide decimal number by 2.
4. Reverse the order of the binary digits in the container.
5. Return the contents of the container as the binary representation.
```

You can implement this algorithm in any programming language of your choice to convert decimal numbers to binary.

Categories C