In
this article I am going to explain how to display prime numbers between 1 to
100 in asp.net
A
number is prime if it is divisible only by one and itself. Number two is the
only even and also the smallest prime number.
Implementation:
C#
code:
protected void Page_Load(object sender, EventArgs e)
{
GetPrimenumber();
}
public void GetPrimenumber()
{
try
{
bool CheckPrime = true;
for (int i = 2; i <= 100; i++)
{
for (int p = 2; p <= 100; p++)
{
if (i != p && i % p ==
0)
{
CheckPrime = false;
break;
}
}
if (CheckPrime)
{
Response.Write(i + "<br/>");
}
CheckPrime = true;
}
}
catch(Exception ex)
{ }
}
VB.net
code:
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
GetPrimenumber()
End Sub
Public Sub GetPrimenumber()
Try
Dim CheckPrime As Boolean = True
For i As Integer = 2 To 100
For p As Integer = 2 To 100
If i <> p AndAlso i Mod p = 0 Then
CheckPrime = False
Exit For
End If
Next
If CheckPrime Then
Response.Write(i + "<br/>")
End If
CheckPrime = True
Next
Catch ex As Exception
End Try
End Sub
0 Comments