Temel C # Dersleri

Merhaba değerli okurlarım, bir çok webmasterın sık sık yardıma ihtiyaç duyduğu döngülerde, dizilerde rahatlıkla sorunlarına çözüm bulmaları amacıyla bu örnekleri hazırladım.

1-Bloklar

static void Main(string[] args)
{
Console.WriteLine();
Console.Write(“Merhaba Dünya”);
Console.ReadLine();
}

2- İlişkisel Opr. ( true-false)

static void Main(string[] args)
{
bool b;
b = 5 > 2;
Console.WriteLine(“b :{0}”,b);
b = 1 > 2;
Console.WriteLine(“b :{0}”, b);
b = 10 == 2;
Console.WriteLine(“b :{0}”, b);
b = 10 != 2;
Console.WriteLine(“b :{0}”, b);
b = 10 + 6 >= 4 * 4;
Console.WriteLine(“b :{0}”,b);
Console.ReadLine();
}

3-Aritmetik Opr. – 1

static void Main(string[] args)
{
int x, y, z;
double d;
x = 9;
y = 2;
z = x / y;
Console.WriteLine(z);
z = 15 / 4;
Console.WriteLine(z);
d = 1.0 / 3;
Console.WriteLine(d);
}

4-Aritmetik Opr. – 2

static void Main(string[] args)
{
int a, b, sonuc;
a = b = 4;
sonuc = ++a * 5;
Console.WriteLine(“{2} {1} {0}”,10,20,30);
Console.WriteLine(“sonuc : {0}”,sonuc);
Console.WriteLine(“a : {0}”,a);
sonuc = b++ * 5;
Console.WriteLine(“sonuc :{0}”,sonuc);
Console.WriteLine(“b :{0}”,b);
}

5- Method Tanımlaması – 1

class aritmetik ( bu özel bir denemeydi dikkate alınması için çok erken)
{
public static int Topla(int a, int b)
{
int sonuc;
sonuc = a + b;
return sonuc;
}
public static int cikar(int a,int b)
{
int sonuc;
sonuc = a – b;
return sonuc;
}
}
static void Main(string[] args)
{
int s;
s = aritmetik.Topla(2, 6);
Console.WriteLine(s);
s = aritmetik.cikar(6, 8);
Console.WriteLine(s);
}

6- Method Tanımlaması – 2

static void Main(string[] args)
{
int val;
double d;
val = Math.Max(100, 39);
Console.WriteLine(val);
d = Math.Pow(3, 2);
Console.WriteLine(d);
d = Math.Sqrt(400);
Console.WriteLine(d);
}

7- Mantıksal Opr. – 1

static void Main(string[] args)
{
bool b, b2, b3;
b = true;
b2 = !b;
Console.WriteLine(“b2 : {0}”, b2);
b3 = !!b;
Console.WriteLine(“b3 : {0}”, b3);
}

8- Mantıksal Opr. – 2

static void Main(string[] args)
{
bool sonuc, sayısalmi;
int x;
char ch;
x = 24;
sonuc = 0 < x && x < 15;
Console.WriteLine(“sonuc : {0}”, sonuc);
ch = ‘5’;
sayısalmi = ch >= ‘0’ && ch < ‘9’;
Console.WriteLine(“sayısal mı : {0}”, sayısalmi);
}

9- Mantıksal Opr. – 3

static void Main(string[] args)
{
char ch; // tek bir karakterde çalışır yanii 15 için olmaz ama 1 için olur.
bool alfabetikmi;
string giris;
Console.WriteLine(“bir karakter gir haciiii”);
giris = Console.ReadLine();
ch = char.Parse(giris);
alfabetikmi = ch > ‘a’ && ch <= ‘z’ || ch >= ‘A’ && ch <= ‘Z’;
Console.WriteLine(“alfabetik mi : {0}” , alfabetikmi); //{0} ifadesi bool da kullanılıyor..
}

10- Özel Opr. – 1

static void Main(string[] args)
{
int k;
k = -5; // negaiflik operatörü
Console.WriteLine(k);
//bilinçli tür dönüşümü
//(type) veriable
int a, b;
double q;
a = 15;
b = 4;
q = a / b;
Console.WriteLine(“q : {0}”, q);
q = (double)a / b; //bilinçli tür dönüştürme
Console.WriteLine(“q : {0}”,q);
// atama oparatoru
int c, d; // a ve b değeri farklı olmasına rağmen en son atamada farklı değer aldığı için
//15 ve 4 değeri artık geçersiz , artık sözkonusu olan a ve b = 125
a = b = c = d = 125;
Console.WriteLine(“a : {0}, b : {1}, c : {2}, d :{3}”, a, b, c, d);
// işlemli atama
int m;
m = 15;
m += 20; // m = m + 20
Console.WriteLine(“m : {0}”, m);
m *= 4; // m = m*4
Console.WriteLine(“m : {0}”, m);
m -= 100; // m = m – 100
Console.WriteLine(“m : {0}”, m);
m /= 2; // m = m / 2
Console.WriteLine(“m : {0}”, m);
// çıkan sonuçlara dikkat et m değeri hep en son işlem değerini alıyor , çünkü atama son işleme yapılıyor.
}

11- Özel Opr. – 2

static void Main(string[] args)
{
int size;
size = sizeof(long);
Console.WriteLine(“size : {0}”, size);
size = sizeof(char);
Console.WriteLine(“size : {0}”, size);
size = sizeof(double);
Console.WriteLine(“size : {0}”, size);
size = sizeof(short);
Console.WriteLine(“size : {0}”, size);
size = sizeof(int);
Console.WriteLine(“size : {0}”, size);
}

12- Özel Opr. – 3

static void Main(string[] args)
{
int max; // koşul ataması , max değeri bulduran bir prg.
int a, b;
string giris;
Console.WriteLine(“a girin :”);
giris = Console.ReadLine();
a = int.Parse(giris);
Console.WriteLine(“b girin :”);
giris = Console.ReadLine();
b = int.Parse(giris);
//koşul doğru ise expr1
//koşul yanlış ise expr2
//koşul ? expr1:expr2
max = a > b ? a : b;
Console.WriteLine(“max : {0}”, max);
// mutlak değeri bulun
int val, mutlakDeger;
val = -90;
mutlakDeger = val < 0 ? -1 * val : val;
Console.WriteLine(“mutlakdeger : {0}”,mutlakDeger);
}

13-Özel Opr. – 4

static void Main(string[] args)
{
// öncelik operatörü
int result;
result = 9 + 5 * 4;
Console.WriteLine(“result : {0}”,result);
result = (9 + 5) * 4;
Console.WriteLine(“result : {0}”, result);
Console.WriteLine(“{0} {1} {2} “, 100,200,300);
Console.WriteLine(“{1} {0} {2} “, 100,200,300);
Console.WriteLine(“{2} {2} {1} “, 100,200,300);
}

14- İf Deyimi – 1

static void Main(string[] args)
{
string giris;
int val;
Console.WriteLine(“bir deger gir”);
giris = Console.ReadLine();
val = int.Parse(giris);
if (val % 2 == 0)
{
Console.WriteLine(“{0} cift sayi” ,val );
}
else
{
Console.WriteLine(“{0} tek sayi” ,val);
}
}

15- İf Deyimi – 2

static void Main(string[] args)
{
string giris;
int val;
Console.WriteLine(“bir sayi gir”);
giris = Console.ReadLine();
val = int.Parse(giris);
if (val>= 0)
{
Console.WriteLine(“{0} pozitif” ,val);
}
else
{
Console.WriteLine(“{0} negatif”,val);
}
}

16- İf Deyimi – 3

static void Main(string[] args)
{
int a, b, c;
string giris;
Console.WriteLine(“a kenar”);
giris = Console.ReadLine();
a = int.Parse(giris);
Console.WriteLine(“b kenar”);
giris = Console.ReadLine();
b = int.Parse(giris);
Console.WriteLine(“c kenar”);
giris = Console.ReadLine();
c = int.Parse(giris);
if (a == b)
{
if (a == c)
{
Console.WriteLine(“eskenar”);
}
else
{
Console.WriteLine(“ikizkenar”);
}
}
else
{
if (b == c)
{
Console.WriteLine(“ikizkenar”);
}
else
{
if (a == c)
{
Console.WriteLine(“ikizkenar”);
}
else
{
Console.WriteLine(“cesitkenar”);
}
}
}
}

17- İf Deyimi – 4

static void Main(string[] args)
{
int a, b, c;
string giris;
Console.WriteLine(“a kenar”);
giris = Console.ReadLine();
a = int.Parse(giris);
Console.WriteLine(“b kenar”);
giris = Console.ReadLine();
b = int.Parse(giris);
Console.WriteLine(“c kenar”);
giris = Console.ReadLine();
c = int.Parse(giris);
if (a == b && a == c)
{
Console.WriteLine(“eskenar”);
}
else
{
if (a != b && a != c && b != c)
{
Console.WriteLine(“cesitkenar”);
}
else
{
Console.WriteLine(“ikizkenar”);
}
}
}

18- For Deyimi – 1

static void Main(string[] args)
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine(“ankara”);
}
Console.WriteLine();
for (int k = 0; k < 5; k++)
{
Console.WriteLine(“van”);
}
}

19- For Deyimi – 2

static void Main(string[] args)
{
int k;
for (k = 0; k < 6; ++k)
{
Console.WriteLine(“mersin :{0}”, k);
}
Console.WriteLine();
}

20- For Deyimi – 3

static void Main(string[] args)

{
int toplam;
toplam = 0;
for (int i = 0; i <= 100; i++)
{
toplam = toplam + i;
}
Console.WriteLine(“toplam : {0}”, toplam);
}

21- For Deyimi – 4

static void Main(string[] args)
{
int matNot;
int gecenSayisi, kalanSayisi;
string giris;
gecenSayisi = 0;
kalanSayisi = 0;
for (int i = 0; i < 7; i++)
{
Console.WriteLine(“Not gir”);
giris = Console.ReadLine();
matNot = int.Parse(giris);
if (matNot >= 50)
{
++gecenSayisi;
}
else
{
++kalanSayisi;
}
}
Console.WriteLine(“Gecenlerin sayisi : {0}”, gecenSayisi);
Console.WriteLine(“Kalanlarin sayisi : {0}”, kalanSayisi);
}

22- For Deyimi – 5

static void Main(string[] args)

int matNot;
int gecenSayisi, kalanSayisi;
string giris;
double gecenNotToplami, kalanNotToplami;
double gecenOrtalama, kalanOrtalama;
gecenSayisi = 0;
kalanSayisi = 0;
gecenNotToplami = kalanNotToplami = 0;
for (int i = 0; i < 7; i++)
{
Console.WriteLine(“Not gir”);
giris = Console.ReadLine();
matNot = int.Parse(giris);
if (matNot >= 50)
{
++gecenSayisi;
gecenNotToplami = gecenNotToplami + matNot;
}
else
{
++kalanSayisi;
kalanNotToplami = kalanNotToplami + matNot;
}
}
Console.WriteLine(“Gecenlerin sayisi : {0}”, gecenSayisi);
Console.WriteLine(“Kalanlarin sayisi : {0}”, kalanSayisi);
if (gecenSayisi > 0)
{
gecenOrtalama = gecenNotToplami / gecenSayisi;
Console.WriteLine(“Gecenlein Ortalamasi : {0}”, gecenOrtalama);
}
else
{
Console.WriteLine(“Gecen yok !”);
}
if (kalanSayisi > 0)
{
kalanOrtalama = kalanNotToplami / kalanSayisi;
Console.WriteLine(“Kalanların Ortalaması : {0}”, kalanOrtalama);
}
}

23- For Deyimi – 6

static void Main(string[] args)
{
int sayi;
int pozitifSayisi, negatifSayisi;
string giris;
double pozitiflerinToplami, negatiflerinToplami;
double pozitifOrtalama, negatifOrtalama;
pozitifSayisi = 0;
negatifSayisi = 0;
pozitiflerinToplami = negatiflerinToplami = 0;
for (int i = 0; i < 6; i++)
{
Console.WriteLine(“Sayı gir”);
giris = Console.ReadLine();
sayi = int.Parse(giris);
if (sayi >= 0)
{
++pozitifSayisi;
pozitiflerinToplami = pozitiflerinToplami + sayi;
}
else
{
++negatifSayisi;
negatiflerinToplami = negatiflerinToplami + sayi;
}
}
Console.WriteLine(“Pozitiflerin sayisi : {0}”, pozitifSayisi);
Console.WriteLine(“Negatiflerin sayisi : {0}”, negatifSayisi);
if (pozitifSayisi > 0)
{
pozitifOrtalama = pozitiflerinToplami / pozitifSayisi;
Console.WriteLine(“Pozitiflerin Ortalamasi : {0}”, pozitifOrtalama);
}
else
{
Console.WriteLine(“Pozitif sayı yok !”);
}
if (negatifSayisi > 0)
{
negatifOrtalama = negatiflerinToplami / negatifSayisi;
Console.WriteLine(“Negatiflerin Ortalaması : {0}”, negatifOrtalama);
}
else
{
Console.WriteLine(“Negatif sayı yok !”);
}
}

24- While Deyimi – 1

static void Main(string[] args)
{
int k;
k = 0;
while (k < 5)
{
Console.WriteLine(“İzmir”);
++k;
}
}

25- While Deyimi – 2

static void Main(string[] args)
{
char secim;
secim = ‘e’;
while (secim == ‘e’)
{
Console.WriteLine(“Adana”);
Console.WriteLine(“Devam etmek istiyor musunuz e / h”);
secim = char.Parse(Console.ReadLine());
}
}

26- While Deyimi – 3

static void Main(string[] args)
{
long k;
string giris;
int len = 0;
Console.WriteLine(“Bir sayı girin”);
giris = Console.ReadLine();
k = long.Parse(giris);
//k = long.Parse(Console.ReadLine());
while (k != 0)
{
++len;
k = k / 10;
}
Console.WriteLine(“Basamak Sayısı : {0}”, len);
}

27- DoWhile Deyimi – 1

static void Main(string[] args)
{
// kosul false olduğu halde 1 kez Ankara yazar
do
{
Console.WriteLine(“Ankara”);
} while (false);
// Asla Trabzon yazmaz (Dönmeyen döngü)
while (false)
{
Console.WriteLine(“Trabzon”);
}
}

28- DoWhile Deyimi – 2

static void Main(string[] args)
{
int a, b, sonuc;
char secim;
do
{
Console.WriteLine(“a degerini girin : “);
a = int.Parse(Console.ReadLine());
Console.WriteLine(“b degerini girin : “);
b = int.Parse(Console.ReadLine());
sonuc = a + b;
Console.WriteLine(“Toplam : {0}”, sonuc);
Console.WriteLine(“Devam etmek istiyor musunuz ? e / h”);
secim = char.Parse(Console.ReadLine());
} while (secim == ‘e’ || secim == ‘E’);
}

29- DoWhile Deyimi – 3

static void Main(string[] args)
{
int a, b, sonuc;
char secim;
bool devamMi;
do
{
Console.WriteLine(“a degerini girin : “);
a = int.Parse(Console.ReadLine());
Console.WriteLine(“b degerini girin : “);
b = int.Parse(Console.ReadLine());
sonuc = a + b;
Console.WriteLine(“Toplam : {0}”, sonuc);
devamMi = false;
do
{
Console.WriteLine(“Devam etmek istiyor musunuz ? e / h”);
secim = char.Parse(Console.ReadLine());
if (secim == ‘e’ || secim == ‘E’ || secim == ‘h’ || secim == ‘H’)
{
devamMi = false;
}
else
{
Console.WriteLine(“Yanlış değer girdiniz…”);
devamMi = true;
}
} while (devamMi);
} while (secim == ‘e’ || secim == ‘E’);
}

30- Faktoriyel Örneği

static void Main(string[] args)
{
int val, faktoryel;
Console.WriteLine(“Faktoryeli bulunacak değeri girin”);
val = int.Parse(Console.ReadLine());
faktoryel = 1;
for (int i = val; i > 0; –i)
{
faktoryel = faktoryel * i;
}
Console.WriteLine(“Faktoryel : {0}”, faktoryel);
}

31- Break Deyimi – 1

static void Main(string[] args)
{
int k;
k = 0;
while (true)
{
if (k == 5)
{
break;
}
Console.WriteLine(“Mersin”);
++k;
}
}

32- Break Deyimi – 2

static void Main(string[] args)
{
int m;
// 0’dan 20’ye kadar sayıları 10 kez yanyana yazdıralım
for (int i = 0; i < 10; i++)
{
m = 0;
while (true)
{
if (m == 21)
{
break;
}
Console.Write(“{0} “, m);
++m;
}
Console.WriteLine();
}
}

33- Continue Deyimi – 1

static void Main(string[] args)
{
int k;
// Sonsuz döngü
k = 0;
while (k < 5)
{
Console.WriteLine(“Ankara : {0}”, k);
continue;
++k; // bu bölümü atlıyoruz, çünkü continue deyimi kullandık, bu yüzden
// k değişkeni asla artmıyor.
}
}

34- Continue Deyimi – 2

static void Main(string[] args)
{
for (int i = 0; i <= 20; i++)
{
if (!(i % 2 == 0))
{
// tek sayılarda döngü sonuna atlıyoruz
continue;
}
// tek sayılarda işlem görmüyor
Console.WriteLine(i);
}
}

35- Goto Deyimi – 1

static void Main(string[] args)
{
int k;
k = 0;
ILK :
Console.WriteLine(“Afyon”);
++k;
if (k < 5)
{
goto ILK;
}
// Yukarıdaki yapının eşdeğeri do – while ile, hangisi daha güzel ?!
// Yukarıdaki kullanım şekli mi, aşağıdaki mi ?
Console.WriteLine();
k = 0;
do
{
Console.WriteLine(“Afyon”);
++k;
} while (k < 5);
}

36- Goto Deyimi – 2

static void Main(string[] args)
{
int m;
for (int i = 0; i < 20; i++)
{
m = 0;
while (true)
{
if (m == 4)
{
goto SON;
}
++m;
}
}
SON:
Console.WriteLine(“Program Sonu…”);
}

37- Switch Case Deyimi – 1

static void Main(string[] args)
{
int gun;
string giris;
Console.WriteLine(“Kaçıncı gün : “);
giris = Console.ReadLine();
gun = int.Parse(giris);
// kod bölümlerini break ile sonlandırıyoruz
switch (gun)
{
case 1: Console.WriteLine(“Pzt.”);
break;
case 2: Console.WriteLine(“Salı”);
break;
case 3: Console.WriteLine(“Crs.”);
break;
case 4: Console.WriteLine(“Pers.”);
break;
case 5: Console.WriteLine(“Cuma”);
break;
case 6: Console.WriteLine(“Cts.”);
break;
case 7: Console.WriteLine(“Pzr.”);
break;
default:
Console.WriteLine(“Yanlış Seçim”);
break;
}
}

38- Switch Case Deyimi – 2

static void Main(string[] args)
{
int gun;
string giris;
char secim;
do
Console.WriteLine(“Kaçıncı gün : “);
giris = Console.ReadLine();
gun = int.Parse(giris);
// kod bölümlerini break ile sonlandırıyoruz
switch (gun)
{
case 1: Console.WriteLine(“Pzt.”);
break;
case 2: Console.WriteLine(“Salı”);
break;
case 3: Console.WriteLine(“Crs.”);
break;
case 4: Console.WriteLine(“Pers.”);
break;
case 5: Console.WriteLine(“Cuma”);
break;
case 6: Console.WriteLine(“Cts.”);
break;
case 7: Console.WriteLine(“Pzr.”);
break;
default:
Console.WriteLine(“Yanlış Seçim”);
break;
}
Console.WriteLine(“Devam etmek istiyor musunuz e / h”);
secim = char.Parse(Console.ReadLine());
} while (secim == ‘e’ || secim == ‘E’);
Console.WriteLine(“Program Sonu…”);
}
39- Switch Case Deyimi – 3

static void Main(string[] args)
{
int a, b;
double sonuc;
char islem;
Console.WriteLine(“a sayısını girin”);
a = int.Parse(Console.ReadLine());
Console.WriteLine(“Islem girin + – * /”);
islem = char.Parse(Console.ReadLine());
Console.WriteLine(“b sayısını girin”);
b = int.Parse(Console.ReadLine());
switch (islem)
{
case ‘+’: sonuc = a + b;
break;
case ‘-‘: sonuc = a – b;
break;
case ‘*’: sonuc = a * b;
break;
case ‘/’: sonuc = (double)a / b;
break;
default:
Console.WriteLine(“Yanlış Seçim”);
sonuc = 0.0;
break;
}
Console.WriteLine(“Sonuc : {0}”, sonuc);
}

40- Switch Case Deyimi – 4

static void Main(string[] args)
{
int ay;
Console.WriteLine(“Kaçıncı ay : “);
ay = int.Parse(Console.ReadLine());
switch (ay)
{
case 12 :
case 1 :
case 2 : Console.WriteLine(“Kış”);
break;
case 3 :
case 4 :
case 5 : Console.WriteLine(“Ilkbahar”);
break;
case 6 :
case 7 :
case 8 : Console.WriteLine(“Yaz”);
break;
case 9 :
case 10 :
case 11 : Console.WriteLine(“Yaz”);
break;
default:
Console.WriteLine(“Yanlış Seçim”);
break;
}
}

41- Diziler – 1

static void Main(string[] args)
{
short[] m = new short[4];
m[0] = 2; // ilk eleman -> 0
m[1] = 4;
m[2] = 10;
m[3] = 50; // son eleman -> m.Length – 1, yani 3
Console.WriteLine(m[0]);
Console.WriteLine(m[1]);
Console.WriteLine(m[2]);
Console.WriteLine(m[3]);
Console.WriteLine();
for (int i = 0; i < 4; i++)
{
Console.WriteLine(m[i]);
}
Console.WriteLine(“Dizi uzunluğu : {0}”, m.Length);
}

42- Diziler – 2

static void Main(string[] args)
{
short[] m = new short[4];
double ort;
double toplam;
for (int i = 0; i < m.Length; i++)
{
m[i] = short.Parse(Console.ReadLine());
}
toplam = 0.0;
for (int i = 0; i < m.Length; i++)
{
toplam = toplam + m[i];
}
ort = toplam / m.Length;
Console.WriteLine(“Ortalama : {0}”, ort);
}

43- Diziler – 3

static void Main(string[] args)
{
int[] k = new int[6] { 2, -1, 10, 70, 9, 23 };
int max;
max = k[0];
for (int i = 1; i < k.Length; i++)
{
if (k[i] > max)
{
max = k[i];
}
}
Console.WriteLine(“maksimum değer : {0}”, max);
}

44- Diziler – 4

// geçenlerin ve kalanların sayısını bulalım
static void Main(string[] args)
{
int[] matNot = new int[7] { 2, 10, 69, 50, 80, 69, 70 };
int gecenler, kalanlar;
gecenler = kalanlar = 0;
for (int i = 0; i < matNot.Length; i++)
{
if (matNot[i] >= 50)
{
++gecenler;
}
else
{
++kalanlar;
}
}
Console.WriteLine(“Gecenlerin sayısı : {0}”, gecenler);
Console.WriteLine(“Kalanların sayısı : {0}”, kalanlar);
}

45- Diziler – 5

static void Main(string[] args)
{
int[] matNot = new int[7]{50, 40, 90, 50, 30, 70, 45};
int gecSayisi, kalSayisi;
double gecenlerToplam, kalanlarToplam;
double gecOrt, kalOrt;
gecSayisi = kalSayisi = 0;
gecenlerToplam = kalanlarToplam = 0.0;
for (int i = 0; i < matNot.Length; i++)
{
if (matNot[i] >= 50)
{
++gecSayisi;
gecenlerToplam = gecenlerToplam + matNot[i];
}
else
{
++kalSayisi;
kalanlarToplam = kalanlarToplam + matNot[i];
}
}
if (gecSayisi > 0)
{
gecOrt = gecenlerToplam / gecSayisi;
Console.WriteLine(“Gecenlerin Sayisi : {0}”, gecSayisi);
Console.WriteLine(“Gecenlerin Ortalamasi : {0}”, gecOrt);
}
else
{
Console.WriteLine(“Gecen yok !…”);
}
if (kalSayisi > 0)
{
kalOrt = kalanlarToplam / kalSayisi;
Console.WriteLine(“Kalanların Sayisi : {0}”, kalSayisi);
Console.WriteLine(“Kalanların Ortalaması : {0}”, kalOrt);
}
else
{
Console.WriteLine(“Kalan yok !…”);
}
}

46- Diziler – 6

// Dizinin elemanlarını takas yapalım, baştakileri, sona
// sondakileri başa alalım, tersyüz edelim
// i ile (length – 1- i) yer değiştirecek
// 0 ile 6. eleman
// 1 ile 5. eleman
// 2 ile 4. eleman
// 3. eleman eşi olmadığı için kimseyle yer değiştirmez !
static void Main(string[] args)
{
int[] m = new int[7] {2, 4, 6, 20, 5, 30, 40 };
int takasSayisi;
int temp;
int length;
length = m.Length; // 7
takasSayisi = m.Length / 2;
for (int i = 0; i < takasSayisi; i++)
{
temp = m[i];
m[i] = m[length – 1 – i];
m[length – 1 – i] = temp;
}
for (int i = 0; i < m.Length; i++)
{
Console.WriteLine(m[i]);
}
}

47- Diziler – 7

// iki diziyi toplayalım
// 1. dizi a
// 2. dizi b
static void Main(string[] args)
{
int[] a = new int[6] { 1, 3, 5, 7, 9, 11 };
int[] b = new int[6] { 2, 4, 6, 8, 10, 12 };
int[] c = new int[6];
for (int i = 0; i < a.Length; i++)
{
c[i] = a[i] + b[i];
}
for (int i = 0; i < c.Length; i++)
{
Console.WriteLine(c[i]);
}
}

48- Diziler – 8

static void Main(string[] args)
{
int[] a = new int[5] { 3, 6, 9, 12, 10 };
int[] d = new int[5];
// d dizisine a dizisinin karelerini yerleştirelim
for (int i = 0; i < d.Length; i++)
{
d[i] = a[i] * a[i];
}
for (int i = 0; i < d.Length; i++)
{
Console.WriteLine(d[i]);
}
}

49- Diziler – 9

// Sort : sıralama demek
// b dizisini küçükten büyüge sıralayalım
static void Main(string[] args)
{
int[] b = new int[7] { 6, 5, 11, 3, 4, 8, 30 };
int temp;
for (int i = 0; i < b.Length – 1; i++)
{
for (int k = i + 1; k < b.Length; k++)
{
if (b[k] < b[i])
{
temp = b[i];
b[i] = b[k];
b[k] = temp;
}
}
}
for (int i = 0; i < b.Length; i++)
{
Console.WriteLine(b[i]);
}
}

50- Takas Örneği

// x ve y değişkenlerinin içeriklerini takas edelim
static void Main(string[] args)
{
int x, y;
int temp;
x = 100;
y = 200;
temp = x; // x’in değerini yedekle
x = y;
y = temp; // yedekleğim değeri de y’e atarım
Console.WriteLine(“x : {0}”, x);
Console.WriteLine(“y : {0}”, y);
}

51- Matris – 1

static void Main(string[] args)
{
int[,] mat = new int[4, 5];
mat[0, 0] = 40;
mat[1, 3] = 5;
mat[3, 4] = 50;
Console.WriteLine(mat[0, 0]);
Console.WriteLine(mat[1, 3]);
Console.WriteLine(mat[3, 4]);
}

52- Matris – 2

static void Main(string[] args)
{
int[,] m = new int[5, 5];
// Bir matrise erişmek için içiçe iki döngü kullanılır
for (int i = 0; i < 5; i++)
{
for (int k = 0; k < 5; k++)
{
// diyagonallere 0 koymak için
if (i == k)
{
m[i, k] = 0;
}
else
{
// matrisin alt bölümüne 1 koymak için
if (i > k)
{
m[i, k] = 1;
}
else // matrisin üst bölümüne 2 koymak için
{
m[i, k] = 2;
}
}
} // for
} // for
for (int i = 0; i < 5; i++)
{
for (int k = 0; k < 5; k++)
{
// sutun bilgilerini yan yana yazdır, yani tek satır
Console.Write(“{0} “, m[i, k]);
}
// her satırı yazdıktan sonra alt satıra geçmek için
Console.WriteLine();
}
}

53- Matris – 3

// Matrislere ilk-değer verme
// Matrisleri dizi dizisi şeklinde düşünmek gerekir
// Her bir diziyi küme parantezleri arasında, birinden
// virgüllerle ayırarak belirtiriz
static void Main(string[] args)
{
int[,] mx = new int[3, 4] {
{2, 4, 6, 8},
{9, 9, 12, 12},
{5, 10, 15, 20}
};
int row, col;
row = mx.GetLength(0);
col = mx.GetLength(1);
for (int i = 0; i < row; i++)
{
for (int k = 0; k < col; k++)
{
Console.Write(“{0, 4}”, mx[i, k]);
}
Console.WriteLine();
}
}

54- Matris – 4

static void Main(string[] args)
{
int[,] a = new int[3, 4] {
{2, 4, 6, 8},
{3, 6, 9, 12},
{5, 10, 15, 20}
};
int[,] b = new int[3, 4] {
{1, 4, 7, 8},
{3, 11, 9, 90},
{15, 15, 15, 20}
};
int[,] c = new int[3, 4];
int row, col;
row = c.GetLength(0);
col = c.GetLength(1);
for (int i = 0; i < row; i++)
{
for (int k = 0; k < col; k++)
{
c[i, k] = a[i, k] + b[i, k];
}
}
for (int i = 0; i < row; i++)
{
for (int k = 0; k < col; k++)
{
Console.Write(“{0, 4}”, c[i, k]);
}
Console.WriteLine();
}
}

55- String – 1

// stringler karakter dizilerdir
static void Main(string[] args)
{
string kent = “Ankara”;
Console.WriteLine(kent[0]);
Console.WriteLine(kent[4]);
string isim = “Ali” + ” ” + “Kemal”;
Console.WriteLine(isim);
Console.WriteLine(isim.Length);
Console.WriteLine(isim.ToUpper());
}