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
- In
main, read the number intonumand create the object withnew BinaryConverter(num). The constructor stores it inbinand setsdecto 0. - Call
isValid(). It returnsfalseifbinis negative or has any digit other than 0 or 1; the message "Invalid input" is then printed. - Call
process(), which callsconvert(bin)and keeps the answer indec.- Base case: if
n == 0,convertreturns 0. - Recursive case: it returns
(int)(n % 10) + 2 * convert(n / 10).
- Base case: if
- 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
| Variable | Data Type | Purpose |
|---|---|---|
bin | long | Data member: the binary number to be converted |
dec | int | Data member: the decimal equivalent of bin |
b | long | Constructor parameter: the binary number passed in |
n (in isValid) | long | Local copy of bin so that bin itself is not changed while its digits are checked |
d | long | Last digit of n in the current pass of the loop |
n (in convert) | long | Parameter: the part of the binary number still to be converted |
args | String[] | Command line arguments of main (not used) |
sc | Scanner | Object used to read the number from the keyboard |
num | long | Number typed by the user in main |
ob | BinaryConverter | Object of the class created in main |
Sample input and output
Sample 1
Enter a binary number: 1101
1101 in binary = 13 in decimalSample 2
Enter a binary number: 10110
10110 in binary = 22 in decimalSample 3
Enter a binary number: 0
0 in binary = 0 in decimalSample 4
Enter a binary number: 1021
Invalid input: a binary number has only the digits 0 and 1Dry run
Input 1101: process() calls convert(1101). The calls go down until n is 0 and the values come back up.
| Call | n | n % 10 | n / 10 | Returns (coming back up) |
|---|---|---|---|---|
convert(1101) | 1101 | 1 | 110 | 1 + 2 * 6 = 13 |
convert(110) | 110 | 0 | 11 | 0 + 2 * 3 = 6 |
convert(11) | 11 | 1 | 1 | 1 + 2 * 1 = 3 |
convert(1) | 1 | 1 | 0 | 1 + 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
- Decimal to Binary "Write a program to convert a decimal number to binary using a recursive method."
- Decimal to Octal "Write a program to convert a decimal number to octal using a recursive method."
- Binary to Decimal using a loop "Write a program to convert a binary number to decimal."