Binary to Decimal: flowchart, algorithm and program

This program reads a binary number typed as an ordinary integer like 1011 and prints its decimal value. It picks off the last digit again and again, multiplies it by the right power of 2, and adds it to a running total.

▶ Watch the dry run step by step →

Algorithm

  1. Step 1: Start.
  2. Step 2: Set decimal = 0, base = 1.
  3. Step 3: Read binary.
  4. Step 4: If binary > 0, go to step 5; otherwise go to step 9.
  5. Step 5: Set digit = binary % 10.
  6. Step 6: Set decimal = decimal + digit * base.
  7. Step 7: Set base = base * 2.
  8. Step 8: Set binary = binary / 10. Go to step 4.
  9. Step 9: Print decimal.
  10. Step 10: Stop.

Pseudocode

BEGIN
    decimal ← 0, base ← 1
    READ binary
    WHILE binary > 0 DO
        digit ← binary % 10
        decimal ← decimal + digit * base
        base ← base * 2
        binary ← binary / 10
    END WHILE
    PRINT decimal
END

Program in C, Python and Java

Binary to Decimal program in C

#include <stdio.h>

int main() {
    int binary, decimal = 0, base = 1, digit;
    printf("Enter a binary number: ");
    scanf("%d", &binary);
    while (binary > 0) {
        digit = binary % 10;
        decimal = decimal + digit * base;
        base = base * 2;
        binary = binary / 10;
    }
    printf("Decimal value = %d\n", decimal);
    return 0;
}

Binary to Decimal program in Python

binary = int(input("Enter a binary number: "))
decimal = 0
base = 1
while binary > 0:
    digit = binary % 10
    decimal = decimal + digit * base
    base = base * 2
    binary = binary // 10
print("Decimal value =", decimal)

Binary to Decimal program in Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int binary, decimal = 0, base = 1, digit;
        System.out.print("Enter a binary number: ");
        binary = sc.nextInt();
        while (binary > 0) {
            digit = binary % 10;
            decimal = decimal + digit * base;
            base = base * 2;
            binary = binary / 10;
        }
        System.out.println("Decimal value = " + decimal);
    }
}

Sample input: binary = 1011
Output: Decimal value = 11

How it works

  • `digit = binary % 10` gives the last digit of the number, which is the lowest binary place (a 0 or a 1).
  • `base` holds the place value of that digit. It starts at 1 (2^0) and `base = base * 2` doubles it after each digit, giving 1, 2, 4, 8, ...
  • `decimal = decimal + digit * base` adds the digit's contribution. A 0 adds nothing and a 1 adds the current `base`.
  • `binary = binary / 10` drops the last digit using integer division, so the loop moves one place to the left each time.
  • The loop `while (binary > 0)` stops when no digits are left, and `decimal` then holds the answer. For 1011: 1 + 2 + 0 + 8 = 11.

Dry run (trace table)

StepLineBoxdecimalbasebinarydigitOutput
13Start
24decimal = 0, base = 101
36Read binary011011
47binary > 0?011011
58digit = binary % 100110111
69decimal = decimal + digit * base1110111
710base = base * 21210111
811binary = binary / 10121011
97binary > 0?121011
108digit = binary % 10121011
119decimal = decimal + digit * base321011
1210base = base * 2341011
1311binary = binary / 1034101
147binary > 0?34101
158digit = binary % 1034100
169decimal = decimal + digit * base34100
1710base = base * 238100
1811binary = binary / 103810
197binary > 0?3810
208digit = binary % 103811

Time and space complexity

Time: O(log n) · Space: O(1). The loop runs once per digit of the number, and only a few variables are used.

Common mistakes

  • Starting `base` at 0 or 2 instead of 1. The rightmost digit has weight 2^0 = 1, so starting elsewhere gives a wrong answer.
  • Forgetting to update `binary = binary / 10`, which makes the loop run forever on the same digit.
  • Entering a number with digits other than 0 and 1 (like 1021). The program does not check this and gives a meaningless result. Very long inputs can also overflow `int`.

Viva questions

Q1. Why is base initialised to 1 and decimal to 0?

base holds the current power of 2, and the rightmost digit has weight 2^0 = 1, so it starts at 1. decimal is the running sum, so it starts at 0 because adding to 0 changes nothing.

Q2. What do binary % 10 and binary / 10 do in this program?

binary % 10 gives the last digit of the number, and binary / 10 is integer division that drops that last digit. Together they peel off the digits from right to left.

Q3. Trace the program for the input 101.

Pass 1: digit=1, decimal=1, base=2, binary=10. Pass 2: digit=0, decimal=1, base=4, binary=1. Pass 3: digit=1, decimal=1+4=5, base=8, binary=0. The loop stops and it prints 5.

Q4. What does the condition binary > 0 check, and what happens if the input is 0 or a negative number?

It keeps looping while there are digits left to process. For 0 the loop never runs and the output is 0, which is correct. For a negative number the loop also never runs, so it wrongly prints 0 with no error message.

Q5. What happens if the user enters a number like 102 that is not binary?

The program does not validate the input. It treats 2 as a digit and calculates 2*1 + 0*2 + 1*4 = 6, which gives a meaningless result. To fix it, check inside the loop that digit is 0 or 1.

Q6. Why is a while loop used here instead of a for loop?

We do not know in advance how many digits the number has. The loop must run until the number becomes 0, so a condition-controlled while loop fits best.

Q7. What is the time complexity of this program?

The loop runs once per digit, so it is O(d), where d is the number of digits in the input. That is O(log10 n) in terms of the numeric value n.

Q8. What would change if you wanted to convert an octal number to decimal instead?

Change base = base * 2 to base = base * 8, since each octal digit is weighted by a power of 8. Extracting digits with % 10 and / 10 stays the same, and the input should only contain digits 0 to 7.

Frequently asked questions

How does this program convert binary to decimal?

It extracts each binary digit with `% 10`, multiplies it by its place value (1, 2, 4, 8, ...) stored in `base`, and adds the results into `decimal`. For 1011 this gives 1 + 2 + 0 + 8 = 11.

Why do we use % 10 and / 10 on a binary number?

The binary number is stored as a normal decimal-looking integer, so `% 10` reads its last digit and `/ 10` removes it. This lets us process the digits from right to left.

What happens if the input is 0 or has digits other than 0 and 1?

For 0 the loop never runs and the output is 0, which is correct. For digits like 2 or 5 the program does not validate them and prints a wrong value, so you would need to add a check if you want safe input.

More programs

← All programs

Try AI Flowchart free →