Design a web application form in ASP.Net with fields for loan amount, interest rate, and duration. Calculate the simple interest and perform necessary validations, ensuring data has been entered for each field and checking for non-numeric values. Use suitable web-form controls.

Answer:

Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SimpleInterestCalculator.Default" %>

<!DOCTYPE html>
<html lang="en">
<head runat="server">
    <meta charset="utf-8" />
    <title>Loan Simple Interest Calculator</title>
</head>
<body>
    <form id="form1" runat="server">
        <h2>Simple Interest Calculator</h2>

        <label for="txtLoanAmount">Loan Amount:</label>
        <asp:TextBox ID="txtLoanAmount" runat="server"></asp:TextBox><br />

        <label for="txtInterestRate">Interest Rate (%):</label>
        <asp:TextBox ID="txtInterestRate" runat="server"></asp:TextBox><br />

        <label for="txtDuration">Duration (years):</label>
        <asp:TextBox ID="txtDuration" runat="server"></asp:TextBox><br />

        <asp:Button ID="btnCalculate" runat="server" Text="Calculate Simple Interest" OnClick="btnCalculate_Click" /><br />

        <label for="lblResult">Calculated Simple Interest:</label>
        <asp:Label ID="lblResult" runat="server" Text="0.00"></asp:Label>
    </form>
</body>
</html>
Default.aspx.cs
using System;
using System.Web.UI;

namespace SimpleInterestCalculator
{
    public partial class Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // No code needed for this example in Page_Load
        }

        protected void btnCalculate_Click(object sender, EventArgs e)
        {
            // Get the input values
            decimal loanAmount = decimal.Parse(txtLoanAmount.Text);
            decimal interestRate = decimal.Parse(txtInterestRate.Text);
            int duration = int.Parse(txtDuration.Text);

            // Calculate the simple interest
            decimal simpleInterest = (loanAmount * interestRate * duration) / 100;

            // Display the result
            lblResult.Text = simpleInterest.ToString("F2");
        }
    }
}
Scroll to Top