Saturday 6 August 2016

write a program to display N odd numbers in asp.net

N_odd.aspx

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>N odd numbers</title>
    <center><h1>N odd numbes</h1></center>
</head>
<body>
    <form id="form1" runat="server">
    <center>
    <div>
         <asp:TextBox ID="txt" runat="server"></asp:TextBox>
         <br />
         <br />
        <asp:Button ID="bt" runat="server" Text="Button" onclick="bt_Click"  />
        <br />
        <br />
        <asp:Label ID="lbl" runat="server" Text="result are shown here"></asp:Label>

    </div>
    </center>
    </form>
</body>
</html>

N_odd.asp.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace our_program
{
    public partial class N_odd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void bt_Click(object sender, EventArgs e)
        {
            int n = Convert.ToInt16(txt.Text);
            int even = 0;
            int j = n * 2;
            string output = "";
            for (int i = 0; i < n * 4; i++)
            {
                if (i % 2 == 1)
                {
                    output = output + "<br>" + i;
                    even++;
                } j = j * 2;
                if (even == n)
                {
                    break;
                }
            }
            lbl.Text = "odd numbers are " + output;
        

        }
    }
}

No comments:

Post a Comment