C Program to Convert Binary Number to Decimal and Vice-Versa
In this tutorial, we’ll explore how to create a C program to convert binary numbers to decimal and vice versa. Binary and decimal are two different number systems commonly used in computer science and mathematics. Binary is base-2, and decimal is base-10. Let’s delve into the conversion process and the C code to achieve this.
Conversion Algorithms
Binary to Decimal Conversion
- Input Binary Number: Accept a binary number from the user.
- Initialize Decimal Result: Initialize a variable to store the decimal result.
- Convert Binary to Decimal: Starting from the rightmost digit, multiply each digit by (where is the position from the right starting with 0) and sum the results.
Decimal to Binary Conversion
- Input Decimal Number: Accept a decimal number from the user.
- Initialize Binary Result: Initialize an array to store the binary result.
- Convert Decimal to Binary: Divide the decimal number by 2 and store the remainder in the array. Repeat the process with the quotient until the quotient becomes 0. The binary equivalent is obtained by reading the array from bottom to top.
C Program for Binary to Decimal and Decimal to Binary Conversion
#include <stdio.h>
// Function to convert binary to decimal
int binaryToDecimal(long long binaryNumber) {
int decimalNumber = 0, i = 0, remainder;
// Convert binary to decimal
while (binaryNumber != 0) {
remainder = binaryNumber % 10;
binaryNumber /= 10;
decimalNumber += remainder * pow(2, i);
++i;
}
return decimalNumber;
}
// Function to convert decimal to binary
void decimalToBinary(int decimalNumber) {
int binaryNumber[100], i = 0;
// Convert decimal to binary
while (decimalNumber != 0) {
binaryNumber[i] = decimalNumber % 2;
decimalNumber /= 2;
++i;
}
// Display binary number
printf("Binary Equivalent: ");
for (int j = i - 1; j >= 0; --j) {
printf("%d", binaryNumber[j]);
}
}
int main() {
int choice, number;
// Menu
printf("Choose an option:\n");
printf("1. Binary to Decimal Conversion\n");
printf("2. Decimal to Binary Conversion\n");
printf("Enter your choice (1/2): ");
scanf("%d", &choice);
switch (choice) {
case 1:
// Binary to Decimal
printf("Enter a binary number: ");
scanf("%lld", &number);
printf("Decimal Equivalent: %d\n", binaryToDecimal(number));
break;
case 2:
// Decimal to Binary
printf("Enter a decimal number: ");
scanf("%d", &number);
decimalToBinary(number);
break;
default:
printf("Invalid choice!\n");
}
return 0;
}
Output Example
Example 1: Binary to Decimal Conversion
Choose an option:
1. Binary to Decimal Conversion
2. Decimal to Binary Conversion
Enter your choice (1/2): 1
Enter a binary number: 1010
Decimal Equivalent: 10
Example 2: Decimal to Binary Conversion
Choose an option:
1. Binary to Decimal Conversion
2. Decimal to Binary Conversion
Enter your choice (1/2): 2
Enter a decimal number: 12
Binary Equivalent: 1100
Explanation
- Binary to Decimal Conversion Function: The
binaryToDecimal
function converts a binary number to decimal using the positional value of digits. - Decimal to Binary Conversion Function: The
decimalToBinary
function converts a decimal number to binary by repeatedly dividing and storing remainders. - Menu: The program provides a menu for the user to choose between binary to decimal and decimal to binary conversion.
- Switch Statement: Based on the user’s choice, the program executes the corresponding conversion.
Conclusion
This C program demonstrates the conversion between binary and decimal numbers. Understanding the algorithm and the provided code can help you perform similar conversions in your programs.
Feel free to reach out if you have any questions or need further clarification!