Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to convert a number from Decimal to Binary using recursion in C#?
Converting a decimal number to binary using recursion in C# involves repeatedly dividing the number by 2 and collecting the remainders. The recursive approach breaks down the problem into smaller subproblems until the base case is reached.
Syntax
Following is the basic syntax for the recursive binary conversion method −
public void ConvertToBinary(int decimalNumber) {
if (decimalNumber > 0) {
ConvertToBinary(decimalNumber / 2);
Console.Write(decimalNumber % 2);
}
}
How It Works
The recursive algorithm works by dividing the decimal number by 2 repeatedly. Each division produces a quotient (used for the next recursive call) and a remainder (which becomes a binary digit). The base case occurs when the number becomes 0.
Using Recursive Method for Display
Example
using System;
public class BinaryConverter {
public void ConvertToBinary(int decimalNumber) {
if (decimalNumber > 0) {
ConvertToBinary(decimalNumber / 2);
Console.Write(decimalNumber % 2);
}
}
}
public class Program {
public static void Main(string[] args) {
BinaryConverter converter = new BinaryConverter();
int decimalNum = 30;
Console.WriteLine("Decimal: " + decimalNum);
Console.Write("Binary: ");
converter.ConvertToBinary(decimalNum);
Console.WriteLine();
// Another example
decimalNum = 45;
Console.WriteLine("Decimal: " + decimalNum);
Console.Write("Binary: ");
converter.ConvertToBinary(decimalNum);
Console.WriteLine();
}
}
The output of the above code is −
Decimal: 30 Binary: 11110 Decimal: 45 Binary: 101101
Using Recursive Method with Return Value
Example
using System;
public class BinaryConverter {
public string GetBinaryString(int decimalNumber) {
if (decimalNumber == 0) {
return "";
}
return GetBinaryString(decimalNumber / 2) + (decimalNumber % 2).ToString();
}
}
public class Program {
public static void Main(string[] args) {
BinaryConverter converter = new BinaryConverter();
int[] numbers = {10, 25, 100};
foreach(int num in numbers) {
string binary = converter.GetBinaryString(num);
if (binary == "") binary = "0"; // Handle zero case
Console.WriteLine($"Decimal {num} = Binary {binary}");
}
}
}
The output of the above code is −
Decimal 10 = Binary 1010 Decimal 25 = Binary 11001 Decimal 100 = Binary 1100100
Using Mathematical Approach with Recursion
Example
using System;
public class BinaryConverter {
public long ConvertToNumericBinary(int decimalNumber) {
if (decimalNumber == 0) {
return 0;
}
return (decimalNumber % 2) + 10 * ConvertToNumericBinary(decimalNumber / 2);
}
}
public class Program {
public static void Main(string[] args) {
BinaryConverter converter = new BinaryConverter();
int decimalNum = 30;
long binaryResult = converter.ConvertToNumericBinary(decimalNum);
Console.WriteLine($"Decimal: {decimalNum}");
Console.WriteLine($"Binary (as number): {binaryResult}");
// Another example
decimalNum = 15;
binaryResult = converter.ConvertToNumericBinary(decimalNum);
Console.WriteLine($"Decimal: {decimalNum}");
Console.WriteLine($"Binary (as number): {binaryResult}");
}
}
The output of the above code is −
Decimal: 30 Binary (as number): 11110 Decimal: 15 Binary (as number): 1111
Conclusion
Converting decimal to binary using recursion in C# involves dividing the number by 2 repeatedly and collecting remainders. The recursive approach naturally handles the reverse order of binary digits, making it an elegant solution for this conversion problem.
