Bu örnekte 1-100 arası rastgele üretilen sayılardan tek sayıların ortalamasını hesaplayıp ekrana yazdıracağız.
Örneğe başlamadan önce C# Rastgele Sayı Üretme sayfasını ziyaret edebilirsiniz.
namespace _35.RastgeleOrnek4
{
class Program
{
static void Main(string[] args)
{
Random random = new Random();
int sayi = 0, toplam = 0, sayac = 0;
for (int i = 1; i <= 10; i++)
{
sayi = random.Next(1, 100);
Console.WriteLine(i + ". sayi = " + sayi);
if (sayi % 2 == 1)
{
toplam += sayi;
sayac++;
// tek sayi adedini tutar..
}
}
Console.WriteLine("Tek sayilarin ortalamasi = " + (toplam / sayac));
Console.ReadLine();
}
}
}
C# dilinde rastgele sayı üretmek için Random sınıfını kullanıyoruz. Bu sınıftan bir tane nesne oluşturup bu nesne ile istediğimiz aralıklarda rastgele sayılar üretiyoruz.
Sayıları üretirken, üretilen sayının tek sayı olup olmadığını kontrol ediyoruz. (Detaylı anlatım için C# Tek-Çift Sayı Bulma sayfasını inceleyebilirsiniz.)
Eğer üretilen sayı tek sayı ise sayıları topluyoruz ve “sayac” değişkenini artırıyoruz. Bu şekilde döngünün sonunda tek sayıların toplamını ve kaç adet olduğunu bulacağız.
En son olarak da toplam değişkenini sayac değişkenine bölerek tek sayıların ortalamasını bulmuş oluyoruz.
Kodun ekran çıktısı şu şekildedir:

27 Yorum
… [Trackback]
[…] There you will find 42990 more Info to that Topic: kodbloklari.com/csharp-rastgele-sayilarla-ornek-4/ […]
… [Trackback]
[…] Info to that Topic: kodbloklari.com/csharp-rastgele-sayilarla-ornek-4/ […]
… [Trackback]
[…] Find More to that Topic: kodbloklari.com/csharp-rastgele-sayilarla-ornek-4/ […]
… [Trackback]
[…] Find More here on that Topic: kodbloklari.com/csharp-rastgele-sayilarla-ornek-4/ […]
… [Trackback]
[…] Read More Information here to that Topic: kodbloklari.com/csharp-rastgele-sayilarla-ornek-4/ […]
… [Trackback]
[…] Read More Information here on that Topic: kodbloklari.com/csharp-rastgele-sayilarla-ornek-4/ […]
… [Trackback]
[…] Info on that Topic: kodbloklari.com/csharp-rastgele-sayilarla-ornek-4/ […]
… [Trackback]
[…] Find More on that Topic: kodbloklari.com/csharp-rastgele-sayilarla-ornek-4/ […]
… [Trackback]
[…] Information to that Topic: kodbloklari.com/csharp-rastgele-sayilarla-ornek-4/ […]
… [Trackback]
[…] Find More Information here to that Topic: kodbloklari.com/csharp-rastgele-sayilarla-ornek-4/ […]
… [Trackback]
[…] Read More Information here to that Topic: kodbloklari.com/csharp-rastgele-sayilarla-ornek-4/ […]
… [Trackback]
[…] Here you will find 79022 more Info to that Topic: kodbloklari.com/csharp-rastgele-sayilarla-ornek-4/ […]
Thanks for sharing. I read many of your blog posts, cool, your blog is very good.
… [Trackback]
[…] Read More Information here on that Topic: kodbloklari.com/csharp-rastgele-sayilarla-ornek-4/ […]
… [Trackback]
[…] Info on that Topic: kodbloklari.com/csharp-rastgele-sayilarla-ornek-4/ […]
… [Trackback]
[…] Info to that Topic: kodbloklari.com/csharp-rastgele-sayilarla-ornek-4/ […]
… [Trackback]
[…] Find More Information here on that Topic: kodbloklari.com/csharp-rastgele-sayilarla-ornek-4/ […]
… [Trackback]
[…] Read More here to that Topic: kodbloklari.com/csharp-rastgele-sayilarla-ornek-4/ […]
… [Trackback]
[…] There you will find 65545 more Information on that Topic: kodbloklari.com/csharp-rastgele-sayilarla-ornek-4/ […]
… [Trackback]
[…] Read More on that Topic: kodbloklari.com/csharp-rastgele-sayilarla-ornek-4/ […]
Maybe Dogecoin is a giant inside joke, and I’m just not in on it. Maybe Dogecoin really is headed to the moon on a crypto rocket ship (both literally and metaphorically). And maybe Musk is such an incredible genius that I just can’t possibly understand what he plans to do with Dogecoin. Dogecoin is different from bitcoin and many other cryptocurrencies in a few key ways. For one thing, there isn’t a finite cap on the number of dogecoins that can be produced. Bitcoin does have such a cap, and its relative scarcity plays a role in its widespread use as a speculative investment. Here’s Why Dogecoin’s (DOGE) Price Can Hit $10 In the Bull Run Not only do I think this crypto will never reach $1, but I wouldn’t invest my hard-earned savings in it at all. In fact, I suspect this token has a better shot at going to zero first. There’s a much better option for investors to choose.
https://milkyway.cs.rpi.edu/milkyway/show_user.php?userid=8024371
Select from 3,000+ Gift Cards and buy them with cryptocurrencies via CoinGate, Binance Pay, or using VISA MasterCard. To verify your card, a small amount of $0.10 SGD (or local equivalent) will be temporarily held for up to 7 days and won’t be charged – please confirm this transaction in order to complete the process. Credit cards come with greater security protections compared to debit cards. It’s best to do your due diligence to research the history and reputation of the exchange, as this is where the transacting will take place (if your bank allows crypto payments). Choosing to buy Bitcoin with a credit card or a debit card is a fast and secure way to acquire some of the most popular cryptocurrency on the market. However, no matter how you choose to perform Bitcoin-related transactions, you can be sure that you’ll receive your BTC in minutes, and that, thanks to the blockchain technology, your transactions will be completely secure and private.
… [Trackback]
[…] Read More on to that Topic: kodbloklari.com/csharp-rastgele-sayilarla-ornek-4/ […]
… [Trackback]
[…] Read More Info here to that Topic: kodbloklari.com/csharp-rastgele-sayilarla-ornek-4/ […]
… [Trackback]
[…] Info on that Topic: kodbloklari.com/csharp-rastgele-sayilarla-ornek-4/ […]
… [Trackback]
[…] Here you will find 94023 more Info to that Topic: kodbloklari.com/csharp-rastgele-sayilarla-ornek-4/ […]
… [Trackback]
[…] Read More Information here to that Topic: kodbloklari.com/csharp-rastgele-sayilarla-ornek-4/ […]
… [Trackback]
[…] Here you will find 55397 more Information on that Topic: kodbloklari.com/csharp-rastgele-sayilarla-ornek-4/ […]