Converting numbers between different bases is a common task in computer science. In this article, we will explore various C Program to Convert Binary Number to Decimal and vice-versa. We will cover multiple approaches with examples, explanations, and outputs.
Prerequisites
Before diving into the examples, you should be familiar with:
- Basic C syntax and structure.
- Understanding of binary and decimal number systems.
- Functions in C.
Understanding the Problem
Binary to Decimal Conversion
Binary numbers are represented in base-2, while decimal numbers are in base-10. Converting binary to decimal involves multiplying each bit by 2 raised to its positional power and summing the results.
Decimal to Binary Conversion
Converting decimal to binary involves repeatedly dividing the number by 2 and recording the remainders.
Example 1: Binary to Decimal Conversion Using Iteration
1.1 Explanation
In this example, we will convert a binary number (represented as a string) to a decimal number using an iterative approach.
1.2 Program: Iterative Binary to Decimal Conversion
#include <stdio.h>
#include <string.h>
#include <math.h>
int binaryToDecimal(char *binary);
int main() {
char binary[65];
printf("Enter a binary number: ");
scanf("%s", binary);
int decimal = binaryToDecimal(binary);
printf("Decimal equivalent of %s is: %d\n", binary, decimal);
return 0;
}
int binaryToDecimal(char *binary) {
int decimal = 0;
int length = strlen(binary);
for (int i = 0; i < length; i++) {
if (binary[length - i - 1] == '1') {
decimal += pow(2, i);
}
}
return decimal;
}
1.3 Output
Enter a binary number: 1101
Decimal equivalent of 1101 is: 13
Example 2: Decimal to Binary Conversion Using Iteration
2.1 Explanation
In this example, we will convert a decimal number to a binary number using an iterative approach. We repeatedly divide the number by 2 and record the remainders.
2.2 Program: Iterative Decimal to Binary Conversion
#include <stdio.h>
void decimalToBinary(int decimal);
int main() {
int decimal;
printf("Enter a decimal number: ");
scanf("%d", &decimal);
printf("Binary equivalent of %d is: ", decimal);
decimalToBinary(decimal);
printf("\n");
return 0;
}
void decimalToBinary(int decimal) {
int binary[32];
int index = 0;
while (decimal > 0) {
binary[index] = decimal % 2;
decimal = decimal / 2;
index++;
}
for (int i = index - 1; i >= 0; i--) {
printf("%d", binary[i]);
}
}
2.3 Output
Enter a decimal number: 13
Binary equivalent of 13 is: 1101
Example 3: Recursive Binary to Decimal Conversion
3.1 Explanation
This example demonstrates converting a binary number to a decimal number using recursion. The recursive function processes each bit and accumulates the result.
3.2 Program: Recursive Binary to Decimal Conversion
#include <stdio.h>
#include <string.h>
int binaryToDecimalRecursive(char *binary, int length);
int main() {
char binary[65];
printf("Enter a binary number: ");
scanf("%s", binary);
int decimal = binaryToDecimalRecursive(binary, strlen(binary));
printf("Decimal equivalent of %s is: %d\n", binary, decimal);
return 0;
}
int binaryToDecimalRecursive(char *binary, int length) {
if (length == 0) {
return 0;
}
int lastDigit = binary[length - 1] - '0';
return (lastDigit * (1 << (strlen(binary) - length))) + binaryToDecimalRecursive(binary, length - 1);
}
3.3 Output
Enter a binary number: 1101
Decimal equivalent of 1101 is: 13
Example 4: Recursive Decimal to Binary Conversion
4.1 Explanation
This example converts a decimal number to binary using recursion. The recursive function handles the division and prints the binary digits.
4.2 Program: Recursive Decimal to Binary Conversion
#include <stdio.h>
void decimalToBinaryRecursive(int decimal);
int main() {
int decimal;
printf("Enter a decimal number: ");
scanf("%d", &decimal);
printf("Binary equivalent of %d is: ", decimal);
decimalToBinaryRecursive(decimal);
printf("\n");
return 0;
}
void decimalToBinaryRecursive(int decimal) {
if (decimal == 0) {
return;
}
decimalToBinaryRecursive(decimal / 2);
printf("%d", decimal % 2);
}
4.3 Output
Enter a decimal number: 13
Binary equivalent of 13 is: 1101
Conclusion
This article covered various C Program to Convert Binary Number to Decimal and vice-versa. We explored iterative and recursive approaches for both conversions, providing a comprehensive understanding of the problem. By understanding these examples, you can tackle similar conversion problems and optimize your solutions for better performance.