C# 0 The Complete Reference


PART I C h a p t e r   8



Download 4,07 Mb.
Pdf ko'rish
bet278/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   274   275   276   277   278   279   280   281   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART I

C h a p t e r   8 :  

A   C l o s e r   L o o k   a t   M e t h o d s   a n d   C l a s s e s  

203


PART IPART I

compute the factorial of a number. For comparison purposes, a nonrecursive equivalent 

is also included.

// A simple example of recursion.

using System;

class Factorial {

  // This is a recursive method.

  public int FactR(int n) {

    int result;

    if(n==1) return 1;

    result = FactR(n-1) * n;

    return result;

  }

  // This is an iterative equivalent.



  public int FactI(int n) {

    int t, result;

    result = 1;

    for(t=1; t <= n; t++) result *= t;

    return result;

  }


}

class Recursion {

  static void Main() {

    Factorial f = new Factorial();

    Console.WriteLine("Factorials using recursive method.");

    Console.WriteLine("Factorial of 3 is " + f.FactR(3));

    Console.WriteLine("Factorial of 4 is " + f.FactR(4));

    Console.WriteLine("Factorial of 5 is " + f.FactR(5));

    Console.WriteLine();

    Console.WriteLine("Factorials using iterative method.");

    Console.WriteLine("Factorial of 3 is " + f.FactI(3));

    Console.WriteLine("Factorial of 4 is " + f.FactI(4));

    Console.WriteLine("Factorial of 5 is " + f.FactI(5));

  }


}

The output from this program is shown here:

Factorials using recursive method.

Factorial of 3 is 6

Factorial of 4 is 24

Factorial of 5 is 120

Factorials using iterative method.

Factorial of 3 is 6

Factorial of 4 is 24

Factorial of 5 is 120

www.freepdf-books.com



204

 

P a r t   I :  



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

The operation of the nonrecursive method 



FactI( )

 should be clear. It uses a loop starting 

at 1 and progressively multiplies each number by the moving product.

The operation of the recursive 




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   274   275   276   277   278   279   280   281   ...   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