C# ile kullanıcının klavyeden girdiği 10 adet sayıdan çift ve teklerin ayrı ayrı toplamları hesaplanıp ekranda gösterilecektir. FOR ve WHILE döngüleri kullanılarak 2 farklı yöntemle çözülmüştür.
FOR döngüsü ile:
1 2 3 4 5 6 7 8 9 10 11 12 | double cifttoplam = 0,tektoplam=0; for (int i = 0; i < 10; i++) { Console.Write("{0}. sayıyı girin:", i + 1); double sayi = Convert.ToDouble(Console.ReadLine()); if (sayi % 2 == 0) cifttoplam = cifttoplam + sayi; else tektoplam = tektoplam + sayi; } Console.WriteLine("Çiftlerin toplamı={0}\nTeklerin toplamı={1}",cifttoplam,tektoplam); Console.ReadKey(); |
WHILE döngüsü ile:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | double cifttoplam = 0,tektoplam=0; int i = 0; while (i < 10) { Console.Write("{0}. sayıyı girin:", i + 1); double sayi = Convert.ToDouble(Console.ReadLine()); if (sayi % 2 == 0) cifttoplam = cifttoplam + sayi; else tektoplam = tektoplam + sayi; i++; } Console.WriteLine("Çiftlerin toplamı={0}\nTeklerin toplamı={1}",cifttoplam,tektoplam); Console.ReadKey(); |