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();
}
}

Join the conversation! Your thoughts help the community grow.