C# Program to Find nCr

This is a C# Program to calculate the value of nCr.

Problem Description

This C# Program Calculates the Value of nCr.

Problem Solution

Here the Combination represented by nCr and each r combination can be arranged in r! different ways. Then the number of r-permutations is equal to the number of r combinations times r!.

Program/Source Code

Here is source code of the C# Program to Calculate the Value of nCr. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

 * C# Program to Calculate the Value of nCr
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication
    class Program
        static void Main(string[] args)
            int n, r,per,fact,fact1,fact2;
            Console.WriteLine("Enter the Value of 'n' and 'r' to " +
                              "find the Permutation :");
            n = Convert.ToInt32(Console.ReadLine());
            r = Convert.ToInt32(Console.ReadLine());
            fact = n;
            for (int i = n - 1; i >= 1; i--)
                fact = fact * i;
            fact2 = r;
            for (int i = r - 1; i >= 1; i--)
                fact2 = fact2 * i;
            int number;
            number = n - r;
            fact1 = number;
            for (int i = number - 1; i >= 1; i--)
                fact1 = fact1 * i;
            fact1 = fact2 * fact1;
            per = fact / fact1;
            Console.WriteLine("Combination : {0}",per);
Program Explanation

In this C# program we are reading the value for ‘n’ and ‘r’ variables respectively. Here the combination represented by nCr and each r combination can be arranged in r! different ways. Then the number of r-permutations is equal to the number of r combination times r!. The algorithm used in this program is
nCr = n! / ((n-r)! r!).


Find all the possible combination for the values n and r. A combination is one or more elements selected from a set without regard to the order. Then ‘ncr’ variable is used to compute
nCr = fact(n) / ( fact(r) * fact(n – r) ).

The fact() function is used to compute the factorial of the value. If-else condition statement is used to check that the argument value in ‘z’ variable is equal to 0. If the condition is true, then execute the statement. Otherwise, if the condition is false then execute the else statement. For loop is used to compute the factorial value.

Initialize the value of ‘i’ variable to 1 and check the condition that the value of ‘i’ variable is less than or equal to the value of argument in ‘z’ variable. If the condition is true then execute the loop. Multiply the value of ‘f’ variable with each value of ‘integer’ in ‘i’ variable. And compute the value for nCr.

Note: Join free Sanfoundry classes at Telegram or Youtube
Runtime Test Cases
Enter the value of 'n' and 'r' to find the Permutation :
Combination : 252

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.