Verilen iki sayı arasında kalan sayıların toplamını bulup veren alternatif bir C# kodu aşağıda verilmiştir.
void IkiSayiArasindakiSayilariTopla(int sayi1, int sayi2)
{
int i;
int toplam;
toplam = 0;
if (sayi1 > sayi2)
{
for (i = sayi2 + 1; i < sayi1; i++)
{
toplam = toplam + i;
}
MessageBox.Show(
sayi2 + " ile " + sayi1
+ " arasındaki sayıların toplamı = "
+ toplam.ToString(),
"SONUÇ",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
else
{
for (i = sayi1 + 1; i < sayi2; i++)
{
toplam = toplam + i;
}
MessageBox.Show(
sayi1 + " ile " + sayi2
+ " arasındaki sayıların toplamı = "
+ toplam.ToString(),
"SONUÇ",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}