.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Visual Studio >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

C# Example: Permutation

Posted By:papsantony       Posted Date: August 13, 2010    Points: 10    Category: Visual Studio    URL: http://www.dotnetspark.com  

C# Example: Permutation
 

An ordered arrangement of objects is called a permutation. The number of permutations of r distinct objects out of n distinct objects is represent by the formula:

P(n, r)

This program calculates the permutation:

using System;

public class Program
{
static long Factorial(long number)
{
if( number <= 1 )
return 1;
else
return number * Factorial(number - 1);
}

static long Permutation(long n, long r)
{
if( r == 0 )
return 0;
if( n == 0 )
return 0;
if( (r >= 0) && (r <= n) )
return Factorial(n) / Factorial(n - r);
else
return 0;
}

static int Main()
{
Console.WriteLine("F(10) = {0}", Factorial(10));
Console.WriteLine("F(6) = {0}\n", Factorial(6));
Console.WriteLine("P(10, 4) = {0}\n", Permutation(10, 4));
return 0;
}
}

This would produce:

F(10) = 3628800

F(6) = 720

P(10, 4) = 5040

Press any key to continue . . .
   

     

Further Readings:

    Responses

    No response found. Be the first to respond this post

    Post Comment

    You must Sign In To post reply
    Find More code samples in C#, ASP.Net, Vb.Net and more Here

    Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend