Users Online

· Guests Online: 39

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

Articles Hierarchy

C# Program to Find nCr

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);
            Console.ReadLine();
          }
    }
}
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!).

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement

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.

 
Runtime Test Cases
 
Enter the value of 'n' and 'r' to find the Permutation :
10
5
Combination : 252

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.78 seconds
10,845,022 unique visits