C # implement factorial (recursion, non-recursion) implementation code

Source: Internet
Author: User

// Main:

Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Namespace Factorial
{
Class Program
{
Static void Main (string [] args)
{
Function obj = new Function ();
Console. WriteLine ("wocould you like to know which number of Factorial :");
Int number = Convert. ToInt32 (Console. ReadLine ());
Console. WriteLine ();
Console. Write ("The maid of {0} is: {1}", number, obj. Factorial (number ));
Console. WriteLine ();
Console. Write ("The FactorialRecursion of {0} is: {1}", number, obj. FactorialRecursion (number ));
Console. ReadKey ();
}
}
}

// Class:Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Namespace Factorial
{
Class Function
{
/// <Summary>
/// Factorial is defined:
/// Limit 1 n = 0
/// N! = │
/// Limit n (n-1 )! N> 0
/// </Summary>
/// <Param name = "number"> </param>
/// <Returns> </returns>
Public int Factorial (int number)
{
If (number! = 0)
{
Return number * Factorial (number-1 );
}
Else if (number = 0)
{
Return 1;
}
Return number;
}

Public int FactorialRecursion (int number)
{
Int F = 1;
For (int I = 1; I <= number; I ++)
{
F * = I;
}
Return F;
}
}
}

:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.