Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

ISC Practicals and Theory - Binary to Decimal (Recursive Technique) (OOP)

Updated 20 Oct 2026

This version converts a binary number to decimal using a recursive method, written as a class in which the number is passed to a parameterised constructor and every method does exactly one job. A binary number has only the digits 0 and 1, and convert() calls itself on a shorter number until nothing is left.

Definition

The value of a binary number is its last digit plus 2 times the value of the remaining digits. The last digit is n % 10 and the remaining digits are n / 10. For example $1101_2 = 1 + 2 \times 110_2 = 13$.

The binary number is typed with its digits together (for example 1101) and read as a long, so up to 19 binary digits are accepted and the decimal result always fits in an int. Input 0 is valid and gives 0. A negative number, or any number with a digit other than 0 or 1, is invalid.

Algorithm

  1. In main, read the number into num and create the object with new BinaryConverter(num). The constructor stores it in bin and sets dec to 0.
  2. Call isValid(). It returns false if bin is negative or has any digit other than 0 or 1; the message "Invalid input" is then printed.
  3. Call process(), which calls convert(bin) and keeps the answer in dec.
    • Base case: if n == 0, convert returns 0.
    • Recursive case: it returns (int)(n % 10) + 2 * convert(n / 10).
  4. Call display() to print the binary number and its decimal equivalent.

Program

The constructor stores the number, isValid() checks it, convert() is the recursive method, process() links the two, and display() prints.

import java.util.*;

/** class BinaryConverter: stores a binary number and converts it to decimal using recursion. */
class BinaryConverter
{
    private long bin;   // binary number to be converted
    private int dec;    // decimal equivalent of bin

    /** Parameterised constructor: stores the binary number and sets dec to 0.
        @param b the binary number */
    BinaryConverter(long b)
    {
        bin = b;
        dec = 0;
    }

    /** Checks the stored number.
        @return true if bin is not negative and has only the digits 0 and 1 */
    boolean isValid()
    {
        long n = bin;                   // copy, so that bin is not destroyed
        if (n < 0)
            return false;
        while (n > 0)
        {
            long d = n % 10;            // last digit
            if (d > 1)
                return false;           // digit 2 to 9 found
            n = n / 10;
        }
        return true;
    }

    /** Recursive method: converts binary to decimal.
        @param n the part of the binary number still to be converted
        @return decimal value of n */
    int convert(long n)
    {
        if (n == 0)
            return 0;                                   // base case
        return (int)(n % 10) + 2 * convert(n / 10);     // recursive case
    }

    /** Calls convert() for the stored number and keeps the result in dec. */
    void process()
    {
        dec = convert(bin);
    }

    /** Displays the binary number and its decimal equivalent. */
    void display()
    {
        System.out.println(bin + " in binary = " + dec + " in decimal");
    }

    /** main: reads the number, creates the object and calls the methods. */
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);            // Scanner object for input
        System.out.print("Enter a binary number: ");
        long num = sc.nextLong();                       // number typed by the user
        BinaryConverter ob = new BinaryConverter(num);  // object of the class
        if (ob.isValid())
        {
            ob.process();
            ob.display();
        }
        else
            System.out.println("Invalid input: a binary number has only the digits 0 and 1");
    }
}

Variable description table

VariableData TypePurpose
binlongData member: the binary number to be converted
decintData member: the decimal equivalent of bin
blongConstructor parameter: the binary number passed in
n (in isValid)longLocal copy of bin so that bin itself is not changed while its digits are checked
dlongLast digit of n in the current pass of the loop
n (in convert)longParameter: the part of the binary number still to be converted
argsString[]Command line arguments of main (not used)
scScannerObject used to read the number from the keyboard
numlongNumber typed by the user in main
obBinaryConverterObject of the class created in main

Sample input and output

Sample 1

Enter a binary number: 1101
1101 in binary = 13 in decimal

Sample 2

Enter a binary number: 10110
10110 in binary = 22 in decimal

Sample 3

Enter a binary number: 0
0 in binary = 0 in decimal

Sample 4

Enter a binary number: 1021
Invalid input: a binary number has only the digits 0 and 1

One value per line

Dry run

Input 1101: process() calls convert(1101). The calls go down until n is 0 and the values come back up.

Callnn % 10n / 10Returns (coming back up)
convert(1101)110111101 + 2 * 6 = 13
convert(110)1100110 + 2 * 3 = 6
convert(11)11111 + 2 * 1 = 3
convert(1)1101 + 2 * 0 = 1
convert(0)0--0 (base case)

dec becomes 13, so the program prints 1101 in binary = 13 in decimal, the same as the real run above.

Variations

  1. Decimal to Binary "Write a program to convert a decimal number to binary using a recursive method."
  2. Decimal to Octal "Write a program to convert a decimal number to octal using a recursive method."
  3. Binary to Decimal using a loop "Write a program to convert a binary number to decimal."