Convert Binary to Decimal in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

/* C - Convert Binary to Decimal */
#include <stdio.h>
#include <math.h>

int main() {
    long long bin;
    int dec = 0, base = 1, rem;
    if (scanf("%lld", &bin) != 1) return 0;
    while (bin > 0) {
        rem = bin % 10;
        dec += rem * base;
        base *= 2;
        bin /= 10;
    }
    printf("%d\n", dec);
    return 0;
}

C Output

Input:
1011

Output:
11



C++ Program

// C++ - Convert Binary to Decimal
#include <iostream>
using namespace std;

int main() {
    long long bin;
    int dec = 0, base = 1;
    if (!(cin >> bin)) return 0;
    while (bin > 0) {
        int rem = bin % 10;
        dec += rem * base;
        base *= 2;
        bin /= 10;
    }
    cout << dec << "\n";
    return 0;
}

C++ Output

Input:
1101

Output:
13



JAVA Program

// Java - Convert Binary to Decimal
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String bin = sc.next();
        int dec = 0, base = 1;
        for (int i = bin.length() - 1; i >= 0; i--) {
            if (bin.charAt(i) == '1') dec += base;
            base *= 2;
        }
        System.out.println(dec);
    }
}

JAVA Output

Input:
10010

Output:
18



Python Program

# Python - Convert Binary to Decimal
bin_str = input()
dec = 0
base = 1
for digit in bin_str[::-1]:
    if digit == '1':
        dec += base
    base *= 2
print(dec)

Python Output

Input:
111

Output:
7



In-Depth Learning – Entire Concept in Paragraphs
Example:
Converting from binary to decimal involves going from a base-2 system (each digit a power of 2) to a base-10 system (powers of 10). In binary, the rightmost digit equal
Addition and subtraction method: a = a + b; b = a - b; a = a - b;
Multiplication and division method: a = a * b; b = a / b; a = a / b; (works only when both are non-zero and no overflow occurs)
XOR method: a ^= b; b ^= a; a ^= b; (bitwise operation, safe from arithmetic overflow but works only for integers)
In Python, tuple unpacking allows swapping in one statement: a, b = b, a.

Real-Life Analogy:
Consider binary as a list of light switches with "1" representing ON and "0" representing OFF. Each switch turns on a bulb of a particular wattage: the first, 1W; then 2W; then 4W; then 8W; and so forth. If switches 8W, 2W, and 1W are ON, the combined brightness is 11W — just like the decimal equivalent for 1011 is 11.

Why It Matters:
Binary is the computer's native tongue. All numbers, characters, or instructions end up as a sequence of 1s and 0s. Having the ability to switch back and forth between binary and decimal aids programmers in debugging low-level operations, comprehending bitwise manipulations, and working with hardware or protocols.

Learning Insights
This course educates you on base conversion, modular arithmetic, and positional value. With each digit being pulled out through % 10 and being multiplied by a rising power of 2, we solidify the understanding of number systems from the ground up. For strings, right-to-left iteration assists in pairing every digit with its proper power of 2.

Interview and Project Relevance
In interviews, binary-to-decimal questions check your knowledge about loops, if-else statements, and base numbers. They can come up in problems such as converting binary inputs, file reading formats, or analyzing network packets. In projects, this conversion is crucial in embedded systems, cryptography, digital electronics, and game development where binary data must be readable.

SEO-friendly conclusion:
This tutorial offers straightforward, effective programs in C, C++, Java, and Python to translate binary numbers into decimal form. By knowing that each binary digit stands for a power of 2 and adding those amounts, you can efficiently do base conversions — a talent essential in programming, data processing, and computer science basics. Whether for interview practice, debugging, or communication with hardware, learning binary-to-decimal conversion reinforces both your programming skills and your knowledge of how computers work with numbers.