Concept: Decimal to Binary Conversion
A decimal number (base 10) can be converted to binary (base 2) by repeatedly dividing the number by 2 and storing the remainders.
Example
Decimal: 10
Steps:
10 ÷ 2 = 5 remainder 0
5 ÷ 2 = 2 remainder 1
2 ÷ 2 = 1 remainder 0
1 ÷ 2 = 0 remainder 1
Binary = 1010 (read remainders in reverse)
Step 1: Design Page – DecimalToBinary.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DecimalToBinary.aspx.cs" Inherits="DecimalToBinary" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Decimal to Binary Converter</title>
<style>
body {
font-family: Arial;
background-color: #f4f7fc;
margin: 50px;
}
.container {
width: 500px;
margin: auto;
background: #fff;
border-radius: 10px;
box-shadow: 0 0 10px #ccc;
padding: 25px;
}
h2 {
text-align: center;
color: #1A2A80;
}
.form-control {
width: 100%;
padding: 8px;
margin-top: 10px;
}
.btn {
background-color: #7A85C1;
color: white;
border: none;
padding: 10px;
border-radius: 5px;
cursor: pointer;
width: 100%;
margin-top: 15px;
}
.btn:hover {
background-color: #5b68a1;
}
.result {
margin-top: 20px;
font-weight: bold;
color: #333;
text-align: center;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="container">
<h2>Decimal to Binary Converter</h2>
<asp:Label ID="lblDecimal" runat="server" Text="Enter Decimal Number:"></asp:Label><br />
<asp:TextBox ID="txtDecimal" runat="server" CssClass="form-control" placeholder="Example: 10"></asp:TextBox><br />
<asp:Button ID="btnConvert" runat="server" Text="Convert to Binary" CssClass="btn" OnClick="btnConvert_Click" /><br />
<asp:Label ID="lblResult" runat="server" CssClass="result"></asp:Label>
</div>
</form>
</body>
</html>
Step 2: Backend Logic – DecimalToBinary.aspx.cs
using System;
using System.Text;
public partial class DecimalToBinary : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnConvert_Click(object sender, EventArgs e)
{
try
{
int decimalNumber = Convert.ToInt32(txtDecimal.Text.Trim());
if (decimalNumber < 0)
{
lblResult.Text = "Please enter a positive number.";
lblResult.ForeColor = System.Drawing.Color.Red;
return;
}
string binary = ConvertToBinary(decimalNumber);
lblResult.Text = "Binary Equivalent of " + decimalNumber + " is: " + binary;
lblResult.ForeColor = System.Drawing.Color.Green;
}
catch
{
lblResult.Text = "Invalid input! Please enter a valid integer.";
lblResult.ForeColor = System.Drawing.Color.Red;
}
}
// Recursive function to convert decimal to binary
private string ConvertToBinary(int num)
{
if (num == 0)
return "0";
else if (num == 1)
return "1";
else
return ConvertToBinary(num / 2) + (num % 2).ToString();
}
}
Example Outputs
| Decimal Input | Binary Output |
|---|
| 5 | 101 |
| 10 | 1010 |
| 25 | 11001 |
| 50 | 110010 |
Algorithm
Step 1: Read the decimal number
Step 2: If number is 0 → return "0"
Step 3: Divide number by 2
Step 4: Store the remainder
Step 5: Repeat Step 3 until number becomes 0
Step 6: Reverse the remainders to get binary number
Step 7: Display result
Example
Input: 10
| Step | num | num/2 | num%2 | Binary |
|---|
| 1 | 10 | 5 | 0 | 0 |
| 2 | 5 | 2 | 1 | 10 |
| 3 | 2 | 1 | 0 | 010 |
| 4 | 1 | 0 | 1 | 1010 |
Output → Binary = 1010
Explanation
Uses recursion to repeatedly divide the decimal number by 2.
The remainders form the binary digits when read in reverse.
The recursive function builds the binary string from top to bottom.
Advantages
Simple and clear recursive logic.
Easily adaptable for other conversions (e.g., Octal, Hexadecimal).
Limitations
Conclusion
This WebForms example demonstrates how to convert a Decimal number to Binary using recursion in C#.
It’s a great real-time exercise to understand recursion, base conversion, and ASP.NET UI interaction.