C# Belirli iki sayı arasındaki asal sayıları bulma

Kullanıcıdan textbox ile alınan iki sayının arasındaki asal sayıları bulup listbox ile ekrana yazdıran örnek.

 

Derlenmiş dosyayı indirmek için tıklayınız…

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ASAL_SAYI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//www.programlamadersleri.com/
}
bool asalmi;
//www.programlamadersleri.com/
private void button1_Click(object sender, EventArgs e)
{
try
{

int toplam = 0;
listBox1.Items.Clear();
int a = Convert.ToInt32(textBox1.Text);
int b = Convert.ToInt32(textBox2.Text);
//www.programlamadersleri.com/
if (b < a)
{
MessageBox.Show("Birinci sayı ikinci sayıdan küçük olmak zorunda.");
}
if (b > a)
{
if (1 != Convert.ToInt32(textBox1.Text))
{
for (; a <= b; a++)
{
asalmi = true;
for (int j = 2; j < a; j++)
{
if (a % j == 0)
{
asalmi = false;
//www.programlamadersleri.com/
}
}
if (asalmi == true)
{
toplam += a;
listBox1.Items.Add(a);
}
}
}
}
//www.programlamadersleri.com/
label3.Text = toplam.ToString();
if (1 >= Convert.ToInt32(textBox1.Text))
{
//www.programlamadersleri.com/
MessageBox.Show("Asal sayılar 2'den başlar..!");
}
}
catch (Exception)
{

MessageBox.Show("Sayı değerlerini kontrol ediniz lütfen..//www.programlamadersleri.com/", "Beklenmedik hata", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Focus();
}
}
}

Bir yanıt yazın