C# 0 The Complete Reference


count , is greater than 5 when the  loop is first entered. This makes the conditional expression,  count < 5



Download 4,07 Mb.
Pdf ko'rish
bet147/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   143   144   145   146   147   148   149   150   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

count

, is greater than 5 when the 

loop is first entered. This makes the conditional expression, 

count < 5

, false from the outset; 

thus, not even one iteration of the loop will occur.

The


for

 loop is most useful when you will be iterating a known number of times. For 

example, the following program uses two 

for

 loops to find the prime numbers between 2 

and 20. If the number is not prime, then its largest factor is displayed.

// Determine if a number is prime. If it is not, then

// display its largest factor.

using System;

class FindPrimes {

  static void Main() {

    int num;

    int i;

    int factor;

    bool isprime;

    for(num = 2; num < 20; num++) {

      isprime = true;

      factor = 0;

      // See if num is evenly divisible.

      for(i=2; i <= num/2; i++) {

        if((num % i) == 0) {

www.freepdf-books.com



94

 

P a r t   I :  



T h e   C #   L a n g u a g e

          // num is evenly divisible. Thus, it is not prime.

          isprime = false;

          factor = i;

        }

      }


      if(isprime)

        Console.WriteLine(num + " is prime.");

      else

        Console.WriteLine("Largest factor of " + num +

                          " is " + factor);

    }


  }

}

The output from the program is shown here:



2 is prime.

3 is prime.

Largest factor of 4 is 2

5 is prime.

Largest factor of 6 is 3

7 is prime.

Largest factor of 8 is 4

Largest factor of 9 is 3

Largest factor of 10 is 5

11 is prime.

Largest factor of 12 is 6

13 is prime.

Largest factor of 14 is 7

Largest factor of 15 is 5

Largest factor of 16 is 8

17 is prime.

Largest factor of 18 is 9

19 is prime.




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   143   144   145   146   147   148   149   150   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish