Tek cift ve asal sayı üretme





private bool Asalmi(int sayi)
{
if (sayi == 1)
{
return false;
}
for (int i = 2; i < sayi - 1; i++)
{
if (sayi % i == 0)
{ return false; }
}
return true;
}
private void button1_Click(object sender, EventArgs e)
{
Random rnd = new Random();
listBox1.Items.Clear();
if (comboBox1.Text == "tek")
{
for (int i = 0; i < 10; i++)
{
int sayi = rnd.Next(0, 100);
if (sayi % 2 != 0)
{
listBox1.Items.Add(sayi);
}
else
{
i--;
}
}
}
if (comboBox1.Text == "çift")
{
for (int i = 0; i < 10; i++)
{
int sayi = rnd.Next(0, 100);
if (sayi % 2 == 0)
{
listBox1.Items.Add(sayi);
}
else
{
i--;
}
}
}
if (comboBox1.Text == "asal")
{
for (int i = 0; i < 10; i++ )
{
int rast = rnd.Next(0, 100);
while (!Asalmi(rast))
{
rast = rnd.Next(0, 100);
}
listBox1.Items.Add(rast);
}
}
}
Diğer bir yöntem
-----------------------------------------------------------------------------------------------------------------------

if (comboBox1.Text == "asal")
{
for (int i = 0; i < 10; i++ )
{
int rast = rnd.Next(0, 100);
if (Asalmi(rast))
{
listBox1.Items.Add(rast);
}
else
{
i--;
}
}
}