✓ Link copied to clipboard
C 📅 July 8, 2026 ⏱ 7 min read

C Program to Convert Binary to Decimal and Vice-Versa

C Program to Convert Binary to Decimal and Vice-Versa

Converting numbers between binary and decimal systems is a common task in computer science. Check out this C program that takes care of both conversions. You can use it to flip binary numbers like 1010 into decimal and back again.

This task comes in handy in systems working with machine-level operations or memory addresses. Let's dive into how you can pull this off with some straightforward logic.

Prerequisites for the C Program to Convert Binary to Decimal

Before diving into the code, ensure you're familiar with these concepts:

  • The modulus operator % to extract digits
  • Looping constructs like while loops
  • Basic understanding of binary and decimal systems
  • Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler

Source Code of C Program to Convert Binary Number to Decimal and vice-versa

Here's the complete code for converting binary numbers to decimal and vice-versa.

binary_decimal_conversion.cC▶ Run
#include <stdio.h>

void convertBinaryToDecimal() {
    int binary, decimal = 0, base = 1, rem;
    // Input binary number
    printf("Enter a binary number: ");
    scanf("%d", &binary);
    // Loop to convert to decimal
    while (binary != 0) {
        rem = binary % 10;
        decimal += rem * base;
        base *= 2;
        binary /= 10;
    }
    printf("Decimal equivalent: %d\n", decimal);
}

void convertDecimalToBinary() {
    int decimal, binary = 0, base = 1, rem;
    // Input decimal number
    printf("Enter a decimal number: ");
    scanf("%d", &decimal);
    // Loop to convert to binary
    while (decimal != 0) {
        rem = decimal % 2;
        binary += rem * base;
        base *= 10;
        decimal /= 2;
    }
    printf("Binary equivalent: %d\n", binary);
}

int main() {
    int choice;
    printf("Choose conversion type: \n1. Binary to Decimal\n2. Decimal to Binary\n");
    printf("Enter your choice (1 or 2): ");
    scanf("%d", &choice);

    if (choice == 1) {
        convertBinaryToDecimal();
    } else if (choice == 2) {
        convertDecimalToBinary();
    } else {
        printf("Invalid choice\n");
    }
    return 0;
}

Try this code yourself — free

Run it in the EduSeekho online compiler. No setup needed.

▶ Open Compiler

Output of the Convert Binary to Decimal Program

Here is what the program outputs when you run it with the input binary number 1010:

consoleoutput
Choose conversion type: 
1. Binary to Decimal
2. Decimal to Binary
Enter your choice (1 or 2): 1
Enter a binary number: 1010
Decimal equivalent: 10
Output of C Program to Convert Binary Number to Decimal and vice-versa
Output of C Program to Convert Binary Number to Decimal and vice-versa

Algorithm to Convert Binary to Decimal in C

The conversion involves processing individual digits of the binary number, or using division for decimal numbers. Here's the step-by-step procedure:

  1. Choose the conversion type: Binary to Decimal or Decimal to Binary.
  2. If converting Binary to Decimal:
    1. Read a binary number.
    2. Initialize variables decimal to 0 and base to 1.
    3. Repeat while the binary number is not 0:
    4. Extract the last bit (digit) using modulus 10.
    5. Add this bit times the base to the decimal number.
    6. Multiply the base by 2.
    7. Remove the last bit from the binary number by dividing by 10.
    8. Output the resulting decimal number.
  3. If converting Decimal to Binary:
    1. Read a decimal number.
    2. Initialize variables binary to 0 and base to 1.
    3. Repeat while the decimal number is not 0:
    4. Calculate the remainder of decimal divided by 2.
    5. Add this remainder times the base to the binary number.
    6. Multiply the base by 10.
    7. Divide the decimal number by 2 to remove the last bit.
    8. Output the resulting binary number.
flowchart TD
  A([Start]) --> B[/Choose conversion type: Binary to Decimal or Decimal to Binary/]
  B --> C{"Type == Binary to Decimal?"}
  C -->|Yes| D[/Input binary number/]
  D --> E[Initialize decimal = 0, base = 1]
  E --> F{binary != 0?}
  F -->|Yes| G[rem = binary %% 10]
  G --> H[decimal = decimal + rem * base]
  H --> I[base = base * 2]
  I --> J[binary = binary / 10]
  J --> F
  F -->|No| K([Output decimal])
  C -->|No| L[/Input decimal number/]
  L --> M[Initialize binary = 0, base = 1]
  M --> N{decimal != 0?}
  N -->|Yes| O[rem = decimal %% 2]
  O --> P[binary = binary + rem * base]
  P --> Q[base = base * 10]
  Q --> R[decimal = decimal / 2]
  R --> N
  N -->|No| S([Output binary])
  K --> T([End])
  S --> T
Flowchart: the algorithm step by step

How the C Program to Convert Binary to Decimal Works

Let's break down the code to understand its functionality more deeply:

  • #include <stdio.h>: This line includes the standard input-output library.
  • void convertBinaryToDecimal(): This function handles conversion from binary to decimal.
  • int binary, decimal = 0, base = 1, rem;: Initializes variables for conversion.
  • scanf("%d", &binary);: Reads the binary input from the user.
  • while (binary != 0): Loops until the binary input has been fully processed.
  • rem = binary % 10: Extracts the last digit of the binary input.
  • decimal += rem * base: Adds the weighted binary digit to the decimal result.
  • base *= 2: Increments the base by a power of 2 for the next digit.
  • binary /= 10: Removes the last digit from the binary input.
  • printf("Decimal equivalent: %d\n", decimal);: Outputs the decimal result.
  • void convertDecimalToBinary(): This function converts decimal to binary.
  • int decimal, binary = 0, base = 1, rem;: Initializes variables for this conversion.
  • scanf("%d", &decimal);: Reads the decimal input from the user.
  • while (decimal != 0): Loops until the decimal number has been converted.
  • rem = decimal % 2: Finds the remainder when divided by 2.
  • binary += rem * base: Adds the remainder in its place value to binary.
  • base *= 10: Multiplies base by 10 to shift left for the next bit.
  • decimal /= 2: Divides decimal by 2 to process the next bit.
  • printf("Binary equivalent: %d\n", binary);: Outputs the binary result.
  • int main(): Main function to execute the program.
  • int choice;: Holds the user's choice for conversion.
  • scanf("%d", &choice);: Takes user input for conversion option.
  • if (choice == 1): Calls the function to convert binary to decimal if chosen.
  • else if (choice == 2): Calls the function to convert decimal to binary if chosen.
  • printf("Invalid choice\n");: Prints this if the choice is invalid.
🧠

Quick Quiz — test your understanding

Tap an option to check your answer

What is the decimal equivalent of binary 1010?

🎉Correct! Binary 1010 is 1*2^3 + 0*2^2 + 1*2^1 + 0*2^0 = 10.
🤔Not quite. Try converting each binary digit to its decimal form.

Time and Space Complexity of the C Program to Convert Binary to Decimal

Let's discuss the efficiency of this program in terms of time and space:

Time Complexity: O(n). Here, n is the number of digits in the binary or decimal number. For instance, a binary number '1010' means four iterations. The program processes each digit once during conversion using loops like while (binary != 0).

Space Complexity: O(1). The program only uses a few integer variables like binary, decimal, base, and rem which do not grow with input size.

⚠️

Common mistake: Confusing binary and decimal inputs can mess up the results. Always make sure you're working with the right number type for the conversion.

Key takeaway: Mastering conversions between binary and decimal is key to understanding low-level data representation in computers.

FAQs About C Program to Convert Binary to Decimal and vice-versa

Here are some common questions about the binary to decimal conversion program:

The program doesn't handle invalid input. It just assumes users give valid binary or decimal numbers. For better apps, add input validation.
No, this program only works for non-negative integers. To handle negative numbers, you'll need extra logic for sign handling.
In binary to decimal conversion, you're multiplying the base by 2 for each binary digit you process. For decimal to binary, multiply the base by 10 to place the binary digits correctly.
Binary's used because it's simple and reliable for computation. Digital electronics can easily tell apart two states, which represent 0 and 1.
You can totally add more functions for other conversions, like octal to binary, or hexadecimal to decimal. Just call them based on what the user picks.
Yep, the conversion is limited by integer size in C. For really large numbers, you’ll need to use types like long long or libraries that can handle big integers.

If you found this program helpful, check out these related programs:

Conclusion

Getting the hang of binary to decimal and vice-versa conversions is fundamental in computer science. It's especially relevant in data processing and optimization. Play around with different inputs and see how the logic holds up. Think about beefing up the program with user input validation to make it tougher. Dig deeper into number systems and data representation for more insights. It'll help you tackle more complex problems.

Practice more with our C quizzes and test out those code modifications in the online compiler.