Translate

ALGORİTMA ve PROGRAMLAMA (C# Sürümü)

Değişken Tanımlama, Aritmetik İşlemler, String'ler,I/O İşlemleri, Metotlar, Diziler (Array), DenetimYapıları (if, for, while, ...), GUI ...



ÖRNEK 1 :  Ekrana Yazdırma Komutu

using System;

class Merhaba

{

public static void Main(string[] args)

    {
Console.WriteLine("Merhaba");
    }
}

Ekran Çıktısı :   Merhaba

________________________________________________


ÖRNEK 2 :  Klavyeden Okuma Komutu ve string

Klavyeden bir metin girilmesini bekler. “Enter” tuşuna basılınca sonlanır.

using System;

class Okuma

{
public static void Main(string[] args)
   {
string ad = Console.ReadLine();
   }
}

________________________________________________

ÖRNEK 3 :   Veri Tipleri, Değişkenler ve İşlemler

using System;

class Degiskenler
{
public static void Main(string[] args)
   {
double d=5.8;
float f = 7.3f;
int i = 5;
float fkare = f*f;
double kareToplam = d*d+f*f+i*i;
Console.WriteLine(kareToplam);
   }
}


Ekran Çıktısı :   111,930002784729

________________________________________________


ÖRNEK 4 :  Tip Dönüşümleri


using System;

class TipDonusum
{
public static void Main(string[] args)
   {
double sayi = 9,1;
double sayi = Double.Parse(Console.ReadLine());
Console.WriteLine("Double : "+Math.Sqrt(sayi)+
" "+"Int : "+(int)Math.Sqrt(sayi));
   }
}

Ekran Çıktısı :   Double : 3,01662062579967 Int : 3

________________________________________________

ÖRNEK 5 :   İki sayıyı toplayan metot ve kullanımını içeren C#
programı


using System;

class Topla
{
public static void Main(string[] args)
   {
Console.WriteLine(topla(5,6));
   }

public static int topla(int sayi1,int sayi2)
  {
return sayi1+sayi2;
  }
}

Ekran Çıktısı :   11


________________________________________________

ÖRNEK 6 : Tamsayı, Döngü, Dizi, Metot ve Ekrana Yazdırma 

int dizi[] = { 5,6,7,8 }; veya benzer şekilde verilen bir tamsayı dizisinin elemanlarının

toplamını bulan metodu içeren C# programını yazınız.


using System;
class DiziTopla
{
public static void Main(string[] args)
   {
int[] dizi = { 5,6,7,8 };
Console.WriteLine(topla(dizi));
   }
public static int topla(int[] dizi)
   {
int toplam = 0;
for(int i=0; i<dizi.Length; ++i)
toplam+=dizi[i];
return toplam;
   }
}

Ekran Çıktısı :   26

________________________________________________



ÖRNEK 7 :   (string'ler) Verilen bir string dizisini, ters sırada (sondan başa doğru)
listeleyen C# programını yazınız.



using System;
class DiziListele{public static void Main(string[] args)   {string[] strDizi={"Ali","Zekiye","Cemil", "Kemal"};int son = strDizi.Length-1;for(int i=son; i>=0; --i)      {Console.WriteLine(strDizi[i]);      }   }}


Ekran Çıktısı :KemalCemilZekiyeAli



_______________________________________________________________________


ÖRNEK 8 :   if, if else


Verilen bir kişi adını bir dizide arayan ve bulunup bulunmadığını belirten C# 
metodunu yazınız

Aranan kişinin string aranan = "Ali" şeklinde verildiğinivarsayabilirsiniz.



using System;

class DiziArama
{

public static void Main(string[] args)

  {

string[] strDizi={"Ali", "Zekiye", "Cemil", "Kemal"};
string kelime = "Cemil";
if (ara(strDizi,kelime))
Console.WriteLine(kelime+" Dizide Bulundu");
else
Console.WriteLine(kelime+" Dizide Bulunamadı");
kelime = "Yılmaz";
if (ara(strDizi,kelime))
Console.WriteLine(kelime+" Dizide Bulundu");
else
Console.WriteLine(kelime+" Dizide Bulunamadı");

}


public static bool ara(string[] dizi, string aranan)
  {

for(int i=0; i<dizi.Length; ++i)
if (aranan.Equals(dizi[i])) return true;
return false;

  }
}


_______________________________________________________________________


ÖRNEK 9 :

Boş bir diziye arka arkaya eleman ekleyen metodu içeren C#
programını yazınız.


using System;

class DiziElemanEkle

{

static string[] strDizi;
static int elemanSayac = 0;

public static void Main(string[] args)

{

strDizi = new String[10];
elemanEkle("Ali");
elemanEkle("Cemil");
listele();

}

public static void elemanEkle(string yeniEleman)

{

strDizi[elemanSayac]=yeniEleman;
elemanSayac++;

}

 public static void listele()

{

for(int i=0; i<strDizi.Length; ++i)
Console.WriteLine(strDizi[i]);

 }

}


________________________________________________________________________

ÖRNEK 10 :

Matrisler

2 x 4'lük bir matris oluşturan ve elemanlarını listeleyen
C# programını yazınız.


using System;

class MatrisListele

{

public static void Main(string[] args)

  {
int[,] matris = { { 5,6,7,8 }, { 9, 10, 11, 12} };
listele(matris);
  }

public static void listele(int[,] matris)

    {

for(int i=0; i<2; ++i)

      {

for(int j=0; j<4; ++j)

Console.Write(matris[i,j]+" ");
Console.WriteLine();

      }
   }

}


______________________________________________________


ÖRNEK 11 : String’ler

using System;

class Stringler
{
public static void Main(string[] args)
{ string s= "abcdefghijklmnopqrstuvwxyzabcde";
// e harfinin alfabedeki konumu
Console.WriteLine(s.IndexOf('e'));
// e harfinin 20. karakterden sonra konumu
Console.WriteLine(s.IndexOf('e',20));
// 5. karakterden 10 karakterlik string parçası
Console.WriteLine(s.Substring(5,10));
// String birle_tirme
Console.WriteLine(String.Concat(s,"ABCDEFG"));
// String atama
s = "Merhaba"; Console.WriteLine(s);
char[] charArray= new char[7];
s.CopyTo(0,charArray,0,7);
Console.WriteLine(charArray);
s = s + new string(charArray);
}
}


Ekran Çıktısı :
4
30
fghijklmno
abcdefghijklmnopqrstuvwxyzab
cdeABCDEFG
Merhaba
Merhaba



_________________________________________________________________________________



ÖRNEK 12 : Mesaj Kutusu Kullanımı

Kullanıcıdan iki tamsayı isteyerek bunların toplamını, çarpımını, farkını, bölümünü
ve bölümünden kalanını bulup sonuçları yazdıran C# programı.


using System;
using System.Windows.Forms;
class MesajKutusu
{
public static void Main(string[] args)
{
string sayi1, sayi2;
int tamsayi1, tamsayi2, toplam, carpim, fark, kalan;
float bolum;
Console.WriteLine("1.sayiyi veriniz");
sayi1=Console.ReadLine();
Console.WriteLine("2.sayiyi veriniz");
sayi2=Console.ReadLine();
tamsayi1 = Int32.Parse(sayi1);
tamsayi2 = Int32.Parse(sayi2);
toplam = tamsayi1+tamsayi2;
carpim = tamsayi1*tamsayi2;
fark = tamsayi1-tamsayi2;
bolum = tamsayi1/tamsayi2;
kalan = tamsayi1%tamsayi2;
MessageBox.Show("Toplam = "+toplam+"\nCarpim = "+carpim+
"\nFark = "+fark+"\nTamsayi Bolum = "+bolum+"\nKalan = "+kalan+
"\nBolum = "+(float)tamsayi1/tamsayi2,
"Sonuclar",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}


Ekran Çıktısı :
1.sayiyi veriniz
5
2.sayiyi veriniz
6


____________________________________________________________________________


ÖRNEK 13 : While Döngüsü Kullanımı

Not ortalamasını bulan C# programı (-1 de_eri girilene kadar notları okur).

using System;
using System.Windows.Forms;
class NotOrt
{
public static void Main(string[] args)
{
float ortalama;
int sayac=0, notu, toplam=0;
Console.WriteLine("Notu giriniz (Exit : -1)");
string str = Console.ReadLine();
notu = Int32.Parse(str);
while(notu!=-1) {
toplam += notu; ++sayac;
Console.WriteLine("Notu giriniz (Exit : -1)");
str = Console.ReadLine();
notu = Int32.Parse(str);
};
string s;
if (sayac==0) s = "Not girilmedi!";
else s = "Sinif ort. = "+(float)toplam/sayac;
MessageBox.Show(s,"Sonuclar",
MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}


Ekran Çıktısı

Notu giriniz (Exit : -1)
5
Notu giriniz (Exit : -1)
6
Notu giriniz (Exit : -1)
-1



 _________________________________________________________________________________



Ardışık(Doğrusal) Arama


using System;
public class LinearSearcher
{
public static void Main(string[] args)
{
int[] dizi = { 1, 3, 5, 7, 9, 11 };
int aranan = Int32.Parse(Console.ReadLine());
int indis = LinearSearch(dizi, aranan);
if (indis!=-1)
Console.WriteLine(indis+" . konumda bulundu");
else
Console.WriteLine("bulunamadı");
}
public static int LinearSearch(int[] dizi, int anahtar)
{
for(int i=0; i<dizi.Length; i++)
if(dizi[i]==anahtar) return i;
return -1;
}
}



Ekran Çıktısı :

4
Bulunamadı

5
2 . konumda bulundu



_________________________________________________________________________________



İkili Arama

using System;
public class BinarySearchTest
{
public static void Main(string[] args)
{
int[] dizi = { 1, 3, 5, 7, 9, 11 };
string mesaj;
int aranan =
Int32.Parse(Console.ReadLine());
int indis = BinarySearch(dizi, aranan);
if (indis!=-1)
mesaj = indis+" . konumda bulundu";
else
mesaj ="bulunamadı";
Console.WriteLine(mesaj);
}
public static int BinarySearch(int[] dizi, int anahtar)
{
int bas=0; int son=dizi.Length-1; int orta;
while(bas<=son)
{
orta = (bas+son)/2;
if (anahtar==dizi[orta]) return orta;
else if(anahtar<dizi[orta])
son = orta-1;
else
bas = orta+1;
}
return -1;
}
}



Ekran Çıktısı :

5
2 . konumda bulundu

4
bulunamadı




_________________________________________________________________________________


Sıralama

using System;
public class BinarySearchTest
{
public static void Main(string[] args)
{
int[] a = { 2,6,4,8,10,12,89,68,45,37 };
string str = "Veriler (Sıralanmadan Önce) :";
for(int i=0; i<a.Length; ++i) str+=" "+a[i];
Console.WriteLine(str);
BubbleSort(a);
str = "Veriler küçükten büyü e sıralı :";
for(int i=0; i<a.Length; ++i) str+=" "+a[i];
Console.WriteLine(str);
}
public static void BubbleSort(int[] b)
{
for(int tur=1; tur<b.Length; ++tur)
for(int i=0; i<b.Length-1; ++i)
if(b[i]>b[i+1]) Swap(b,i);
}
public static void Swap(int[] c, int ilk)
{
int temp=c[ilk];
c[ilk]=c[ilk+1];
c[ilk+1]=temp;
}
}



Ekran Çıktısı

Veriler (Sıralanmadan Önce) : 2 6 4 8 10 12 89 68 45 37
Veriler küçükten büyüğe sıralı : 2 4 6 8 10 12 37 45 68 89




_________________________________________________________________________________


 Seçmeli Sıralama

using System;
class SelectionSort
{
public static void Main(string[] args)
{
int i, j, tur, yer;
string[] dizi =
{ "Ali", "Cemil", "Veli", "Abdullah", "Kemal" };
string enkVeri, temp;
Console.WriteLine();
for(i=0; i<5; ++i)
Console.Write(dizi[i]+" ");
for(tur=0; tur<4; ++tur)
{
enkVeri = dizi[tur];
yer = tur;
for(j=tur+1; j<5; ++j)
{
if(dizi[j].CompareTo(enkVeri)<0)
{ yer = j;
enkVeri=dizi[yer]; };
}
temp = dizi[tur];
dizi[tur] = enkVeri;
dizi[yer] = temp;
};
Console.WriteLine();
for(i=0; i<5; ++i)
Console.Write(dizi[i]+" ");
}
}


Ekran Çıktısı

Ali Cemil Veli Abdullah Kemal

Abdullah Ali Cemil Kemal Veli





____________________________________________________________________________________________________________

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication1

{
    class Program
    {
        static void Main(string[] args)
        { 

//Kullanıcı tarafından ardışık artan iki sayı girilinceye kadar girilen sayıların toplamını bulan programı yazın.


            int toplam = 0;
            int son = 0;
            int sayac = 1;
            while (true)
            {
                Console.Write("Sayı girin:");
                int sayi = Convert.ToInt32(Console.ReadLine());
                toplam = toplam + sayi;
                if (sayac == 1) son = sayi;
                else
                {
                    if (sayi - son == 1) break;
                    else son = sayi;
                }
                sayac = sayac + 1;
            }
            Console.Write(toplam);
            Console.Read();
            /*
            Console.Write("Bir sayı girin:");
            int sayi1 = Convert.ToInt32(Console.ReadLine());
            Console.Write("Diğer sayıyı girin:");
            int sayi2 = Convert.ToInt32(Console.ReadLine());
            for (int i = sayi1; i < sayi2; i++)
            {
                int sayac = 0;
                for (int j = 1; j < i; j++)
                {
                    if (i % j == 0) sayac = sayac + 1;
                }
                if (sayac==2) Console.WriteLine(i);
            }
            Console.Read();
            /*
            int sonuc = 1;
            Console.Write("Sayıyı girin:");
            int sayi = Convert.ToInt32(Console.ReadLine());
            Console.Write("üssünü girin:");
            int üs = Convert.ToInt32(Console.ReadLine());
            //Console.Write("Sonuc:" + Math.Pow(sayi, üs));
            for (int i = üs; i>=1;i--)
            {
               sonuc = sonuc * sayi;
            }
           Console.Write(sonuc);
            Console.Read();
 
   
            /*

            int s = 0;
            int ts = 0;
            int[] d = { 33, 2, 11,8,10,12 };
            for (int i = 0; i < d.Length; i++)
            {
                if (d[i] % 2 == 1) ts++;
            }
            int[] ydizi = new int[ts];
            for (int i = 0; i < d.Length; i++)
            {
                if (d[i] % 2 == 1)
                {
                    ydizi[s] = d[i];
                    s++;
                }
            }
            for (int i = 0; i < ydizi.Length; i++) Console.WriteLine(ydizi[i]);
            Console.Read();

            /*

            int[] d1 = { 33, 2, 11, 5 };
            int[] d2 = new int[d1.Length];
            for (int i = 0; i < d1.Length; i++)
            {
                if (d1[i] % 2 == 1)
                {
                    d2[i] = d1[i];
                }
            }
            for (int j = 0; j < d2.Length; j++)
            {
                Console.WriteLine(d2[j]);
            }

            /*

            int sonuc = 1;
            Console.Write("Sayıyı girin:");
            int sayi = Convert.ToInt32(Console.ReadLine());
            for (int i = sayi; i >= 1; i--)
            {
                sonuc = sonuc * i;
            }
            Console.WriteLine("************");
            Console.Write("{0} sayısının faktöryeli:{1}", sayi, sonuc);
            Console.Read();
            /*
            int sayi1 = Convert.ToInt32(Console.ReadLine());
            int sayi2 = Convert.ToInt32(Console.ReadLine());
            string sonuc = "";
            int fakt = 1;
            int sonuctop = 0;
            for (int i = sayi1; i <= sayi2; i++)
            {
                for (int j = 1; j <= i; j++)
                {
                    fakt = fakt * j;

                    sonuc += i.ToString() + " != " + fakt.ToString() + "\n";
                    sonuctop += fakt;
                }
                    Console.WriteLine("Faktoriyellerin Toplamı : " + sonuctop);
                    fakt = 1;
           
            }



     
            /*
            Console.Write("1. Sayı : ");
            int s1 = int.Parse(Console.ReadLine());
            Console.Write("2. Sayı : ");
            int s2 = Convert.ToInt32(Console.ReadLine());
            for (int i = s1; i <= s2; i++)
            {
                int kontrol = 0;
                for (int j = 2; j < i; j++)
                {
                    if (i % j == 0)
                    {
                        kontrol = 1;
                        break;
                    }
                }
                if (kontrol == 1)
                {
                    Console.WriteLine("{0} asal değidir", i);
                }
                else
                {
                    Console.WriteLine("{0} asaldır.", i);
                }
            }
            Console.ReadKey();


            /*


 //a. birinci satırını kullanıcı girecek


//b. ikinci satırı random atanacak


//c. 3. satırı, 1. satırın tersi olacak

//d. 1.satırdaki en büyük elemanı yazdıracak


int[,] d = new int[3,5];
            Random r = new Random();
            int[] tdizi = new int[5];
            int buyuk = 0;
            for (int j = 0; j < 5; j++)
            {
           
                d[0, j] = Convert.ToInt32(Console.ReadLine());
                tdizi[j] = d[0, j];
                d[1, j] = r.Next(10);
                buyuk = d[0, 0];
                if (d[0, j] > buyuk) buyuk = d[0, j];
             
            }
            for (int i = 0; i < 5; i++)
            {



                d[2,5 - 1 - i] = tdizi[i];
            }
            Console.WriteLine();
           for(int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    Console.Write(d[i, j]+"  ");
                }
                Console.WriteLine();
       
            }
          Console.WriteLine(buyuk);
            Console.Read();


         
         
            /*



            int[,] d = new int[2, 3];
            Random r = new Random();
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    d[i, j] = r.Next(10, 100);
                }
            }
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    Console.Write(d[i, j] + "  ");
                }
                Console.WriteLine();
            }




            /*

            int[] d1 = { 1,  };
            int[] d2 = { 3, 4, 5,10,9 };
            int[] db = new int[d1.Length + d2.Length];
            for (int i = 0; i < d1.Length; i++) db[i] = d1[i];
            int s = d1.Length;
            for (int i = 0; i < d2.Length; i++)
            {
                bool drm = false;
                for (int j = 0; j < d1.Length; j++) if (d2[i] == d1[j]) drm = true;
                if (drm == false)
                {
                    db[s] = d2[i];
                    s++;
                }
            }
            for (int i = 0; i < db.Length; i++) Console.Write(db[i] + " ");

            /*


            int[] d = new int[10];
            Random r = new Random();
            int s = 0;
            int tek = 0;
            while (tek<=10)
            {
                for (int i = 0; i < d.Length; i++)
                {
                    s = r.Next(100);
                    if (s % 2 == 1)
                    {
                        tek ++;
                        d[i] = s;
                    }
                    else tek --;
                }
            }
            for (int i = 0; i < d.Length; i++) Console.Write(d[i] + "  ");
            Console.Read();
            /*
            int[] dizi = new int[5];
            int kucuk = 0;
            int sayi = 0;
            Console.Write("Sayı:");
            dizi[0] = Convert.ToInt32(Console.ReadLine());
            sayi = dizi[0];
            kucuk = sayi;
            for (int i = 1; i < dizi.Length; i++)
            {
                Console.Write("Sayı:");
                dizi[i] = Convert.ToInt32(Console.ReadLine());
                if (dizi[i] > sayi) sayi = dizi[i];
                if (dizi[i] < kucuk) kucuk = dizi[i];
            }
            Console.WriteLine("En büyük sayı: " + sayi);
            Console.WriteLine("En küçük sayı: " + kucuk);

            /*


            int[] dizi = new int[5];
            Random r = new Random();
            for (int i = 0; i < 5; i++)
            {
                dizi[i] = r.Next(10);
                for (int j = 0; j < i; j++)
                {
                    if (dizi[j] == dizi[i])
                    {
                        i--;
                        break;
                    }
                }
            }
            for (int i = 0; i < 5; i++)
            {
                Console.Write(dizi[i] + "  ");
            }



            /*


            int[] dizi = { 11, 12, 13, 14, 15, 16, 17, 18, 19};
            for (int i = dizi.Length -1 ; i > 0; i--)
            {
                Console.Write(dizi[i]  + "  ");
            }
   


            /*


            int[] dizi = new int[5];
            Random rastgele = new Random();
            for (int i = 0; i < dizi.Length; i++)
            {
                dizi[i] = rastgele.Next(0, 100);
            }
            Array.Sort(dizi);
            Array.Reverse(dizi);
            for (int i = 0; i < dizi.Length; i++)
            {
                Console.Write(dizi[i] + "  ");
            }



            /*


            int[] dizi = new int[10];
            for (int i = 0; i < dizi.Length; i++)
            {
                Console.Write("Klavyeden {0}. sayıyı girin:", i + 1);
                int sayi = Convert.ToInt32(Console.ReadLine());
                if (sayi > 100)
                    dizi[i] = sayi;
            }
            for (int i = 0; i < dizi.Length; i++)
            {
                Console.Write(dizi[i] + "  ");
            }


          /*


         int[] sayilar = { 4,5,1,6 };
            double toplam = 0, ort = 0;
            for (int i = 0; i < sayilar.Length; i++)
            {
                toplam += sayilar[i];
            }
            ort = toplam / sayilar.Length;
            Console.WriteLine("Ortalama:" + ort);
     
            /*
            int[] sayilar = new int[10];
            for (int i = 0; i < sayilar.Length; i++)
            {
                Console.Write("Klavyeden {0}. sayıyı girin:", i + 1);
                sayilar[i] = Convert.ToInt32(Console.ReadLine());
            }
            Array.Sort(sayilar);
            for (int i = 0; i < sayilar.Length; i++)
            {
                Console.WriteLine(sayilar[i]);
            }


            */


            /*
            Console.Write("Satır sayisi:");
            int sat = Convert.ToInt32(Console.ReadLine());
            Console.Write("Sutun sayısı:");
            int sut = Convert.ToInt32(Console.ReadLine());
            int[,] dizi = new int[sat, sut];
            int[,] yenidizi = new int[sat, sut];
            Random r = new Random();
            for (int i = 0; i < sat; i++)
            {
                for (int j = 0; j < sut; j++)
                {
                    dizi[i, j] = r.Next(10, 100);
                }
            }
            for (int i = 0; i < sat; i++)
            {
                for (int j = 0; j < sut; j++)
                {
                    Console.Write(dizi[i, j] + "  ");
                }
                Console.WriteLine();
            }
            for (int i = 0; i < sat; i++)
            {
                for (int j = 0; j < sut; j++)
                {
                    yenidizi[i, j] = dizi[i, sut - 1 - j];
                }
            }

            Console.WriteLine("**************");
            for (int i = 0; i < sat; i++)
            {
                for (int j = 0; j < sut; j++)
                {
                    Console.Write(yenidizi[i, j] + "  ");
                }
                Console.WriteLine();
            }


             * */
        }
    }
}







______________________________________________________


ÖRNEK 11 : String’ler

using System;

class Stringler
{
public static void Main(string[] args)
{ string s= "abcdefghijklmnopqrstuvwxyzabcde";
// e harfinin alfabedeki konumu
Console.WriteLine(s.IndexOf('e'));
// e harfinin 20. karakterden sonra konumu
Console.WriteLine(s.IndexOf('e',20));
// 5. karakterden 10 karakterlik string parçası
Console.WriteLine(s.Substring(5,10));
// String birle_tirme
Console.WriteLine(String.Concat(s,"ABCDEFG"));
// String atama
s = "Merhaba"; Console.WriteLine(s);
char[] charArray= new char[7];
s.CopyTo(0,charArray,0,7);
Console.WriteLine(charArray);
s = s + new string(charArray);
}
}


Ekran Çıktısı :
4
30
fghijklmno
abcdefghijklmnopqrstuvwxyzab
cdeABCDEFG
Merhaba
Merhaba



_________________________________________________________________________________



ÖRNEK 12 : Mesaj Kutusu Kullanımı

Kullanıcıdan iki tamsayı isteyerek bunların toplamını, çarpımını, farkını, bölümünü
ve bölümünden kalanını bulup sonuçları yazdıran C# programı.


using System;
using System.Windows.Forms;
class MesajKutusu
{
public static void Main(string[] args)
{
string sayi1, sayi2;
int tamsayi1, tamsayi2, toplam, carpim, fark, kalan;
float bolum;
Console.WriteLine("1.sayiyi veriniz");
sayi1=Console.ReadLine();
Console.WriteLine("2.sayiyi veriniz");
sayi2=Console.ReadLine();
tamsayi1 = Int32.Parse(sayi1);
tamsayi2 = Int32.Parse(sayi2);
toplam = tamsayi1+tamsayi2;
carpim = tamsayi1*tamsayi2;
fark = tamsayi1-tamsayi2;
bolum = tamsayi1/tamsayi2;
kalan = tamsayi1%tamsayi2;
MessageBox.Show("Toplam = "+toplam+"\nCarpim = "+carpim+
"\nFark = "+fark+"\nTamsayi Bolum = "+bolum+"\nKalan = "+kalan+
"\nBolum = "+(float)tamsayi1/tamsayi2,
"Sonuclar",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}


Ekran Çıktısı :
1.sayiyi veriniz
5
2.sayiyi veriniz
6


____________________________________________________________________________


ÖRNEK 13 : While Döngüsü Kullanımı

Not ortalamasını bulan C# programı (-1 de_eri girilene kadar notları okur).

using System;
using System.Windows.Forms;
class NotOrt
{
public static void Main(string[] args)
{
float ortalama;
int sayac=0, notu, toplam=0;
Console.WriteLine("Notu giriniz (Exit : -1)");
string str = Console.ReadLine();
notu = Int32.Parse(str);
while(notu!=-1) {
toplam += notu; ++sayac;
Console.WriteLine("Notu giriniz (Exit : -1)");
str = Console.ReadLine();
notu = Int32.Parse(str);
};
string s;
if (sayac==0) s = "Not girilmedi!";
else s = "Sinif ort. = "+(float)toplam/sayac;
MessageBox.Show(s,"Sonuclar",
MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}


Ekran Çıktısı

Notu giriniz (Exit : -1)
5
Notu giriniz (Exit : -1)
6
Notu giriniz (Exit : -1)
-1



 _________________________________________________________________________________



Ardışık(Doğrusal) Arama


using System;
public class LinearSearcher
{
public static void Main(string[] args)
{
int[] dizi = { 1, 3, 5, 7, 9, 11 };
int aranan = Int32.Parse(Console.ReadLine());
int indis = LinearSearch(dizi, aranan);
if (indis!=-1)
Console.WriteLine(indis+" . konumda bulundu");
else
Console.WriteLine("bulunamadı");
}
public static int LinearSearch(int[] dizi, int anahtar)
{
for(int i=0; i<dizi.Length; i++)
if(dizi[i]==anahtar) return i;
return -1;
}
}



Ekran Çıktısı :

4
Bulunamadı

5
2 . konumda bulundu



_________________________________________________________________________________



İkili Arama

using System;
public class BinarySearchTest
{
public static void Main(string[] args)
{
int[] dizi = { 1, 3, 5, 7, 9, 11 };
string mesaj;
int aranan =
Int32.Parse(Console.ReadLine());
int indis = BinarySearch(dizi, aranan);
if (indis!=-1)
mesaj = indis+" . konumda bulundu";
else
mesaj ="bulunamadı";
Console.WriteLine(mesaj);
}
public static int BinarySearch(int[] dizi, int anahtar)
{
int bas=0; int son=dizi.Length-1; int orta;
while(bas<=son)
{
orta = (bas+son)/2;
if (anahtar==dizi[orta]) return orta;
else if(anahtar<dizi[orta])
son = orta-1;
else
bas = orta+1;
}
return -1;
}
}



Ekran Çıktısı :

5
2 . konumda bulundu

4
bulunamadı




_________________________________________________________________________________


Sıralama

using System;
public class BinarySearchTest
{
public static void Main(string[] args)
{
int[] a = { 2,6,4,8,10,12,89,68,45,37 };
string str = "Veriler (Sıralanmadan Önce) :";
for(int i=0; i<a.Length; ++i) str+=" "+a[i];
Console.WriteLine(str);
BubbleSort(a);
str = "Veriler küçükten büyü e sıralı :";
for(int i=0; i<a.Length; ++i) str+=" "+a[i];
Console.WriteLine(str);
}
public static void BubbleSort(int[] b)
{
for(int tur=1; tur<b.Length; ++tur)
for(int i=0; i<b.Length-1; ++i)
if(b[i]>b[i+1]) Swap(b,i);
}
public static void Swap(int[] c, int ilk)
{
int temp=c[ilk];
c[ilk]=c[ilk+1];
c[ilk+1]=temp;
}
}



Ekran Çıktısı

Veriler (Sıralanmadan Önce) : 2 6 4 8 10 12 89 68 45 37
Veriler küçükten büyüğe sıralı : 2 4 6 8 10 12 37 45 68 89




_________________________________________________________________________________


 Seçmeli Sıralama

using System;
class SelectionSort
{
public static void Main(string[] args)
{
int i, j, tur, yer;
string[] dizi =
{ "Ali", "Cemil", "Veli", "Abdullah", "Kemal" };
string enkVeri, temp;
Console.WriteLine();
for(i=0; i<5; ++i)
Console.Write(dizi[i]+" ");
for(tur=0; tur<4; ++tur)
{
enkVeri = dizi[tur];
yer = tur;
for(j=tur+1; j<5; ++j)
{
if(dizi[j].CompareTo(enkVeri)<0)
{ yer = j;
enkVeri=dizi[yer]; };
}
temp = dizi[tur];
dizi[tur] = enkVeri;
dizi[yer] = temp;
};
Console.WriteLine();
for(i=0; i<5; ++i)
Console.Write(dizi[i]+" ");
}
}


Ekran Çıktısı

Ali Cemil Veli Abdullah Kemal

Abdullah Ali Cemil Kemal Veli