ISC Practicals and Theory - Binary to Decimal (Recursive Technique)
Updated 20 Oct 2026
A binary number uses only the digits 0 and 1. Converting it to decimal means adding up each digit multiplied by the matching power of 2. This note does the conversion with a recursive method, toDecimal(), that calls itself on a shorter number each time, without Math.pow, Integer.parseInt or any other library shortcut.
Definition
In a binary number the rightmost digit has place value $2^0$, the next $2^1$, and so on. For example:
- $1101_2 = 1 \times 8 + 1 \times 4 + 0 \times 2 + 1 \times 1 = 13$
- $10110_2 = 16 + 0 + 4 + 2 + 0 = 22$
The recursive idea: the value of a binary number is its last digit plus 2 times the value of the remaining digits. So $1101_2 = 1 + 2 \times 110_2$, and $110_2 = 0 + 2 \times 11_2$, and so on. The remaining digits are obtained by n / 10 and the last digit by n % 10.
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
- In
main, create an objectobof classBinToDecand callaccept()to read the binary number intobin. - Call
isBinary(bin). It returnsfalseif the number is negative or any digit is other than 0 or 1.- If it returns
false, print the "Invalid input" message and stop.
- If it returns
- Otherwise call
toDecimal(bin)and store the result indec.- Base case: if
n == 0, return 0. - Recursive case: return
(int)(n % 10) + 2 * toDecimal(n / 10). - Each call works on a number with one digit less, so the calls must reach 0.
- Base case: if
- Call
display()to print the decimal equivalent.
Program
accept() reads the number, isBinary() validates it, toDecimal() is the recursive method, and display() prints the answer.
import java.util.*;
/** class BinToDec: converts a binary number to its decimal equivalent using recursion. */
class BinToDec
{
long bin; // binary number entered by the user
int dec; // decimal equivalent of bin
/** Default constructor: initialises the data members. */
BinToDec()
{
bin = 0;
dec = 0;
}
/** Reads the binary number from the user. */
void accept()
{
Scanner sc = new Scanner(System.in); // Scanner object for input
System.out.print("Enter a binary number: ");
bin = sc.nextLong();
}
/** Checks the number: returns true if it is not negative and every digit is 0 or 1.
@param n the number to check
@return true if n is a valid binary number, otherwise false */
boolean isBinary(long n)
{
if (n < 0)
return false;
while (n > 0) // peel off the digits one by one
{
long d = n % 10; // last digit of n
if (d != 0 && d != 1)
return false; // a digit other than 0 or 1 found
n = n / 10; // drop the last digit
}
return true;
}
/** Recursive method: converts a binary number to decimal.
Base case: n == 0 gives 0.
Recursive case: last digit + 2 * (value of the remaining digits).
@param n binary number
@return decimal value of n */
int toDecimal(long n)
{
if (n == 0)
return 0; // base case
return (int)(n % 10) + 2 * toDecimal(n / 10); // recursive case
}
/** Displays the decimal equivalent. */
void display()
{
System.out.println("Decimal equivalent = " + dec);
}
/** main: creates the object and calls the methods. */
public static void main(String args[])
{
BinToDec ob = new BinToDec(); // object of the class
ob.accept();
if (ob.isBinary(ob.bin))
{
ob.dec = ob.toDecimal(ob.bin);
ob.display();
}
else
System.out.println("Invalid input: a binary number has only the digits 0 and 1");
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
bin | long | Data member: the binary number entered by the user |
dec | int | Data member: the decimal equivalent of bin |
sc | Scanner | Object used to read the number from the keyboard |
n (in isBinary) | long | Parameter: the number being checked; its last digit is dropped in each pass of the loop |
d | long | Last digit of n in the current pass of the loop |
n (in toDecimal) | long | Parameter: the binary number (or the part of it) still to be converted |
args | String[] | Command line arguments of main (not used) |
ob | BinToDec | Object of the class created in main |
Sample input and output
Sample 1
Enter a binary number: 1101
Decimal equivalent = 13Sample 2
Enter a binary number: 10110
Decimal equivalent = 22Sample 3
Enter a binary number: 0
Decimal equivalent = 0Sample 4
Enter a binary number: 1021
Invalid input: a binary number has only the digits 0 and 1Dry run
Input 1101. The calls go down until n becomes 0, then the values come back up.
Calls going down
| Call | n | n % 10 | n / 10 | Next call |
|---|---|---|---|---|
| 1 | 1101 | 1 | 110 | toDecimal(110) |
| 2 | 110 | 0 | 11 | toDecimal(11) |
| 3 | 11 | 1 | 1 | toDecimal(1) |
| 4 | 1 | 1 | 0 | toDecimal(0) |
| 5 | 0 | - | - | base case, returns 0 |
Values returning up
| Call | Expression | Returns |
|---|---|---|
| 5 | base case | 0 |
| 4 | 1 + 2 * 0 | 1 |
| 3 | 1 + 2 * 1 | 3 |
| 2 | 0 + 2 * 3 | 6 |
| 1 | 1 + 2 * 6 | 13 |
dec becomes 13, so the program prints Decimal equivalent = 13, the same as the real run above.
Variations
- Decimal to Binary "Write a program to convert a decimal number to binary using a recursive method."
- Binary to Decimal using a loop "Write a program to convert a binary number to decimal."
- Decimal to Octal "Write a program to convert a decimal number to octal using a recursive method."