nCr Factor of the given number

Write a program to find out NCR factor of given number.

For example

Input :
n = 5, r = 2

Output :
30

Solution:

using System;

namespace Ncr
{
    class Program
    {
        public static void Main()
        {
            Console.WriteLine("Enter the n and r values:");
            var n = Convert.ToInt32(Console.ReadLine());
            var r = Convert.ToInt32(Console.ReadLine());

            var factor = Factor(n) / (Factor(r) * Factor(n - r));
            Console.WriteLine(factor);
            Console.ReadLine();
        }

        static int Factor(int n)
        {
            var res = 1;
            for (var i = 2; i <= n; i++)
                res *= i;

            return res;
        }

    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *