C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet186/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   182   183   184   185   186   187   188   189   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

LeastComFactor( )

, which returns the smallest factor that its two 

arguments have in common. In other words, it returns the smallest whole number value 

that can evenly divide both arguments.

// Add a method that takes two arguments.

using System;

class ChkNum {

  // Return true if x is prime.

  public bool IsPrime(int x) {

    if(x <= 1) return false;

    for(int i=2; i <= x/i; i++)

      if((x %i) == 0) return false;

    return true;

  }


  // Return the least common factor.

  public int LeastComFactor(int a, int b) {

    int max;

    if(IsPrime(a) || IsPrime(b)) return 1;

    max = a < b ? a : b;

    for(int i=2; i <= max/2; i++)

      if(((a%i) == 0) && ((b%i) == 0)) return i;

www.freepdf-books.com




124

 

P a r t   I :  



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

    return 1;

  }

}

class ParmDemo {



  static void Main() {

    ChkNum ob = new ChkNum();

    int a, b;

    for(int i=2; i < 10; i++)

      if(ob.IsPrime(i)) Console.WriteLine(i + " is prime.");

      else Console.WriteLine(i + " is not prime.");

    a = 7;

    b = 8;

    Console.WriteLine("Least common factor for " +

                      a + " and " + b + " is " +

                      ob.LeastComFactor(a, b));

    a = 100;

    b = 8;

    Console.WriteLine("Least common factor for " +

                      a + " and " + b + " is " +

                      ob.LeastComFactor(a, b));

    a = 100;

    b = 75;

    Console.WriteLine("Least common factor for " +

                      a + " and " + b + " is " +

                      ob.LeastComFactor(a, b));

  }


}

Notice that when 




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   182   183   184   185   186   187   188   189   ...   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