Tuesday 26 November 2013

How to Check a given number is prime or not?

How to check a given number is prime or not?









ASPX page Code:-
------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="primeNumber.aspx.cs" Inherits="primeNumber" %>

<!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></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
    </style>
    <script type="text/javascript" language="javascript">

        function valid() {
            var txtval = document.getElementById("txtnum").value;
            if (txtval == "" || txtval == null) {
                alert('Please Enter Value');
                document.getElementById("txtnum").focus();
                return false;
              

               
            }
        }
   
    </script>
</head>
<body>
    <form id="form1" runat="server">
     <br />
      
        <br /> <br />
      
        <br /> <br />
      
        <br /> <br />
      
        <br /> <br />
      
        <br />
    <div style="border:3px solid blue; width:30%;margin-left:auto;margin-right:auto;">
        <br />
      
        <br />
        <table class="style1">
            <tr>
                <td colspan="2" style="text-align: center">
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
                </td>
            </tr>
            <tr>
                <td align="right">
                    Enter&nbsp; a Number</td>
                <td align="left">
        <asp:TextBox ID="txtnum" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td align="right">
                    &nbsp;</td>
                <td align="left">
        <asp:Button ID="Button1" runat="server" Text="Check Prime or not?" onclick="Button1_Click" OnClientClick="return valid()"/>
                </td>
            </tr>
            <tr>
                <td align="right">
                    &nbsp;</td>
                <td align="left">
                    &nbsp;</td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

ASPX.CS page Code:-
----------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class primeNumber : System.Web.UI.Page
{
   
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (isPrime(Convert.ToInt32(txtnum.Text)) == true)
        {
            Label1.Text = "Number is prime" + txtnum.Text;
        }
        else
        {
            Label1.Text = "Number is not prime" + txtnum.Text;
        }
    }
    public static bool isPrime(int num)
    {
        bool primnum = true;
        int factor = num -1;


        for(int i=2;i<=factor; i++)
        {
            if (num % i == 0)
            {
                primnum = false ;
            }
        }
        return primnum;
    }
}

3 comments:

Unknown said...
This comment has been removed by a blog administrator.
Ramu kumar Gupta said...
This comment has been removed by the author.
Ramu kumar Gupta said...

Yes Darling you r right but you hold the the conditional value in one extra variable and then you are checking this extra variable.