Step 1: Design Page – ArmstrongNumber.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ArmstrongNumber.aspx.cs" Inherits="ArmstrongNumber" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Armstrong Number Checker - Real Time Example</title>
<style>
body {
font-family: Arial;
background-color: #f0f2f5;
margin: 50px;
}
.container {
width: 420px;
margin: auto;
background: white;
border-radius: 8px;
box-shadow: 0px 0px 10px #ccc;
padding: 20px;
}
h2 {
color: #1A2A80;
text-align: center;
}
.form-control {
width: 100%;
padding: 8px;
margin-top: 10px;
}
.btn {
background-color: #7A85C1;
color: white;
border: none;
padding: 10px 20px;
margin-top: 10px;
border-radius: 5px;
cursor: pointer;
}
.result {
font-weight: bold;
color: #333;
margin-top: 15px;
text-align: center;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="container">
<h2>Armstrong Number Checker</h2>
<asp:Label ID="lblNumber" runat="server" Text="Enter a Number:"></asp:Label><br />
<asp:TextBox ID="txtNumber" runat="server" CssClass="form-control" placeholder="Example: 153"></asp:TextBox><br />
<asp:Button ID="btnCheck" runat="server" Text="Check Armstrong" CssClass="btn" OnClick="btnCheck_Click" /><br />
<asp:Label ID="lblResult" runat="server" CssClass="result"></asp:Label>
</div>
</form>
</body>
</html>
Step 2: Backend Logic – ArmstrongNumber.aspx.cs
using System;
public partial class ArmstrongNumber : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnCheck_Click(object sender, EventArgs e)
{
int number;
bool isValid = int.TryParse(txtNumber.Text.Trim(), out number);
if (!isValid || number < 0)
{
lblResult.Text = "Please enter a valid positive number.";
lblResult.ForeColor = System.Drawing.Color.Red;
return;
}
if (IsArmstrong(number))
{
lblResult.Text = $"{number} is an Armstrong Number.";
lblResult.ForeColor = System.Drawing.Color.Green;
}
else
{
lblResult.Text = $"{number} is NOT an Armstrong Number.";
lblResult.ForeColor = System.Drawing.Color.Red;
}
}
private bool IsArmstrong(int num)
{
int original = num;
int sum = 0;
int digits = num.ToString().Length; // Count number of digits
while (num > 0)
{
int digit = num % 10;
sum += (int)Math.Pow(digit, digits); // Add power of each digit
num /= 10;
}
return sum == original;
}
}
Real-Time Example Flow
Open ArmstrongNumber.aspx in your browser.
Enter 153 and click “Check Armstrong”.
The result displays:
“153 is an Armstrong Number.”
If you enter 123, output:
“123 is NOT an Armstrong Number.”
Explanation
| Step | Description |
|---|
| 1 | Take the input number from the user. |
| 2 | Count total digits. |
| 3 | Separate each digit using modulus %. |
| 4 | Raise each digit to the power of total digits and add them. |
| 5 | Compare the sum with the original number. |
| 6 | If equal → Armstrong number, else not. |
Sample Input / Output
| Input | Calculation | Result |
|---|
| 153 | 1³ + 5³ + 3³ = 153 | Armstrong |
| 370 | 3³ + 7³ + 0³ = 370 | Armstrong |
| 371 | 3³ + 7³ + 1³ = 371 | Armstrong |
| 9474 | 9⁴ + 4⁴ + 7⁴ + 4⁴ = 9474 | Armstrong |
| 123 | 1³ + 2³ + 3³ = 36 | Not Armstrong |