C# For Döngüsü

C# dili for döngüsü kullanarak farklı örnekler yaptık.İşte örnekler;

1′den 1000′e kadar olan sayıların toplamını bulup sonucu ekranda gösteren program

int toplam = 0;
for (int i = 0; i <= 1000; i++)
{
toplam += i;
}

Console.WriteLine("Toplam = {0}", toplam);
Console.ReadKey();

Girilen iki sayını arasındaki sayıların toplamını bulan program

int toplam=0,a,b;

Console.WriteLine("bir sayi girin");
a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("bir sayi girin");
b = Convert.ToInt32(Console.ReadLine());

for (int i = a; i <= b; i++)
{

toplam += i;

}

Console.WriteLine("Toplam : {0}", toplam);

Console.ReadKey();

İstenilen Sayıyı 1000kez Yazdırma

for (int i = 0; i <= 1000; i++)
{
Console.WriteLine("istediğiniz yazıyı yazabilirsiniz...\n");
}

Console.ReadKey();

Tek Sayıları Yazdırma

for (int i = 1; i <= 100; i+=2)
{
Console.WriteLine("{0}.sayı",i);
}

Console.ReadKey();

1 ile 1000 Arasındaki Sayıların Toplamının Ortalaması

int i;
double toplam=0;
for (i = 1; i <= 1000; i++)
{
toplam += i;
}
toplam /= 1000;
Console.WriteLine(toplam);
Console.ReadKey();

100 ile 200 Arasındaki Çift Sayıların Toplamının Ortalaması

int i;
double toplam = 0;
for (i = 100; i <= 200; i+=2)
{
toplam += i;
Console.WriteLine(toplam);
}
toplam /= i;
Console.WriteLine(toplam);
Console.ReadKey();

A’dan Z’ye Kadar Ekrana Yazdırma

char i;
for (i = 'a'; i <= 'z'; i++)
{
Console.WriteLine(i);
}
Console.ReadKey();

İstenilen Sayı Kadar Girilen Sayıların Ortalaması

int i,sayi,deger;
double toplam=0;
Console.WriteLine("kaç sayi gireceksiniz");
deger= Convert.ToInt32(Console.ReadLine());
for (i = 1; i<=deger; i++)
{
Console.WriteLine(i + ".sayiyi giriniz...");
sayi = Convert.ToInt32(Console.ReadLine());
toplam += sayi;

}
toplam /= deger;

Console.WriteLine("ortalama : " + toplam);

Console.ReadKey();


C# Girilen Sayıların Tek-Çift Sayısının Bulunması

C# if-else kullanarak girilen sayıların tek-çift sayısının bulunması

int a, b, c,tek_sayi=0,cift_sayi=0 ;
Console.WriteLine("1. sayi girin");
a = Convert.ToInt32(Console.ReadLine());
if (a % 2 == 0)
{
++cift_sayi;
}
else
{
++tek_sayi;

}
Console.WriteLine("2. sayi girin");
b = Convert.ToInt32(Console.ReadLine());
if (b % 2 == 0)
{
++cift_sayi;
}
else
{
++tek_sayi;

}
Console.WriteLine("3. sayi girin");
c = Convert.ToInt32(Console.ReadLine());
if (c % 2 == 0)
{
++cift_sayi;
}
else
{
++tek_sayi;

}

Console.WriteLine("tek sayilar={0},çift sayilar={1}", tek_sayi, cift_sayi);
Console.ReadLine();
1 2 3