Welcome :Guest

Congratulations!!!

Top 5 Contributors of the Month
yasminpriya
Ram
Gaurav Pal
christianasteves

 Home >> Code Snippets >> Visual Studio >> Post New Resource

# 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) = 3628800F(6) = 720P(10, 4) = 5040Press any key to continue . . .`