# Calculate value of ncr efficiently

Here given code implementation process.

``````/*
Java program
Calculate value of ncr efficiently
*/
public class Combinations
{
// Returns the gcd value of two numbers
public long gcd(long a, long b)
{
if (b == 0)
{
return a;
}
return gcd(b, a % b);
}
public void findNcR(int objects, int subset)
{
int n = objects;
int r = subset;
// Formula of nCr
// nCr = n!/[r!(n-r)!]
// Here
// C : Indicates number of combination
// n : Number of objects in set
// r : Subset size
long p = 1;
long k = 1;
if (n - r < r)
{
// Change r when (n - r) is smaller to r
r = n - r;
}
if (r != 0)
{
while (r >= 1)
{
p = p * n;
k = k * r;
// Reduce the value of  n and r by 1
n--;
r--;
// Get gcd of p and k
long d = gcd(p, k);
// Divide p and k by d
// d is common divisor
p = p / d;
k = k / d;
}
}
else
{
p = 1;
}
// Display given value
System.out.println(" Given n : " + objects + " r : " + subset);
// Display calculated result
System.out.println(" Result  : " + p);
}
public static void main(String[] args)
{
// Test Case
}
}``````

#### input

`````` Given n : 7 r : 2
Result  : 21
Given n : 12 r : 3
Result  : 220``````
``````// Include header file
#include <iostream>
using namespace std;

/*
C++ program
Calculate value of ncr efficiently
*/

class Combinations
{
public:
// Returns the gcd value of two numbers
long gcd(long a, long b)
{
if (b == 0)
{
return a;
}
return this->gcd(b, a % b);
}
void findNcR(int objects, int subset)
{
int n = objects;
int r = subset;
// Formula of nCr
// nCr = n!/[r!(n-r)!]
// Here
// C : Indicates number of combination
// n : Number of objects in set
// r : Subset size
long p = 1;
long k = 1;
if (n - r < r)
{
// Change r when (n - r) is smaller to r
r = n - r;
}
if (r != 0)
{
while (r >= 1)
{
p = p *n;
k = k *r;
// Reduce the value of  n and r by 1
n--;
r--;
// Get gcd of p and k
long d = this->gcd(p, k);
// Divide p and k by d
// d is common divisor
p = p / d;
k = k / d;
}
}
else
{
p = 1;
}
// Display given value
cout << " Given n : " << objects << " r : " << subset << endl;
// Display calculated result
cout << " Result  : " << p << endl;
}
};
int main()
{
// Test Case
return 0;
}``````

#### input

`````` Given n : 7 r : 2
Result  : 21
Given n : 12 r : 3
Result  : 220``````
``````// Include namespace system
using System;
/*
Csharp program
Calculate value of ncr efficiently
*/
public class Combinations
{
// Returns the gcd value of two numbers
public long gcd(long a, long b)
{
if (b == 0)
{
return a;
}
return this.gcd(b, a % b);
}
public void findNcR(int objects, int subset)
{
int n = objects;
int r = subset;
// Formula of nCr
// nCr = n!/[r!(n-r)!]
// Here
// C : Indicates number of combination
// n : Number of objects in set
// r : Subset size
long p = 1;
long k = 1;
if (n - r < r)
{
// Change r when (n - r) is smaller to r
r = n - r;
}
if (r != 0)
{
while (r >= 1)
{
p = p * n;
k = k * r;
// Reduce the value of  n and r by 1
n--;
r--;
// Get gcd of p and k
long d = this.gcd(p, k);
// Divide p and k by d
// d is common divisor
p = p / d;
k = k / d;
}
}
else
{
p = 1;
}
// Display given value
Console.WriteLine(" Given n : " + objects + " r : " + subset);
// Display calculated result
Console.WriteLine(" Result  : " + p);
}
public static void Main(String[] args)
{
// Test Case
}
}``````

#### input

`````` Given n : 7 r : 2
Result  : 21
Given n : 12 r : 3
Result  : 220``````
``````<?php
/*
Php program
Calculate value of ncr efficiently
*/
class Combinations
{
// Returns the gcd value of two numbers
public	function gcd(\$a, \$b)
{
if (\$b == 0)
{
return \$a;
}
return \$this->gcd(\$b, \$a % \$b);
}
public	function findNcR(\$objects, \$subset)
{
\$n = \$objects;
\$r = \$subset;
// Formula of nCr
// nCr = n!/[r!(n-r)!]
// Here
// C : Indicates number of combination
// n : Number of objects in set
// r : Subset size
\$p = 1;
\$k = 1;
if (\$n - \$r < \$r)
{
// Change r when (n - r) is smaller to r
\$r = \$n - \$r;
}
if (\$r != 0)
{
while (\$r >= 1)
{
\$p = \$p * \$n;
\$k = \$k * \$r;
// Reduce the value of  n and r by 1
\$n--;
\$r--;
// Get gcd of p and k
\$d = \$this->gcd(\$p, \$k);
// Divide p and k by d
// d is common divisor
\$p = \$p / \$d;
\$k = \$k / \$d;
}
}
else
{
\$p = 1;
}
// Display given value
echo(" Given n : ".\$objects.
" r : ".\$subset.
"\n");
// Display calculated result
echo(" Result  : ".\$p.
"\n");
}
}

function main()
{
// Test Case
}
main();``````

#### input

`````` Given n : 7 r : 2
Result  : 21
Given n : 12 r : 3
Result  : 220``````
``````/*
Node JS program
Calculate value of ncr efficiently
*/
class Combinations
{
// Returns the gcd value of two numbers
gcd(a, b)
{
if (b == 0)
{
return a;
}
return this.gcd(b, a % b);
}
findNcR(objects, subset)
{
var n = objects;
var r = subset;
// Formula of nCr
// nCr = n!/[r!(n-r)!]
// Here
// C : Indicates number of combination
// n : Number of objects in set
// r : Subset size
var p = 1;
var k = 1;
if (n - r < r)
{
// Change r when (n - r) is smaller to r
r = n - r;
}
if (r != 0)
{
while (r >= 1)
{
p = p * n;
k = k * r;
// Reduce the value of  n and r by 1
n--;
r--;
// Get gcd of p and k
var d = this.gcd(p, k);
// Divide p and k by d
// d is common divisor
p = p / d;
k = k / d;
}
}
else
{
p = 1;
}
// Display given value
console.log(" Given n : " + objects + " r : " + subset);
// Display calculated result
console.log(" Result  : " + p);
}
}

function main()
{
// Test Case
}
main();``````

#### input

`````` Given n : 7 r : 2
Result  : 21
Given n : 12 r : 3
Result  : 220``````
``````#    Python 3 program
#    Calculate value of ncr efficiently
class Combinations :
#  Returns the gcd value of two numbers
def gcd(self, a, b) :
if (b == 0) :
return a

return self.gcd(b, a % b)

def findNcR(self, objects, subset) :
n = objects
r = subset
#  Formula of nCr
#  nCr = n!/[r!(n-r)!]
#  Here
#  C : Indicates number of combination
#  n : Number of objects in set
#  r : Subset size
p = 1
k = 1
if (n - r < r) :
#  Change r when (n - r) is smaller to r
r = n - r

if (r != 0) :
while (r >= 1) :
p = p * n
k = k * r
#  Reduce the value of  n and r by 1
n -= 1
r -= 1
#  Get gcd of p and k
d = self.gcd(p, k)
#  Divide p and k by d
#  d is common divisor
p = int(p / d)
k = int(k / d)

else :
p = 1

#  Display given value
print(" Given n : ", objects ," r : ", subset)
#  Display calculated result
print(" Result  : ", p)

def main() :
#  Test Case

if __name__ == "__main__": main()``````

#### input

`````` Given n :  7  r :  2
Result  :  21
Given n :  12  r :  3
Result  :  220``````
``````#    Ruby program
#    Calculate value of ncr efficiently
class Combinations
#  Returns the gcd value of two numbers
def gcd(a, b)
if (b == 0)
return a
end

return self.gcd(b, a % b)
end

def findNcR(objects, subset)
n = objects
r = subset
#  Formula of nCr
#  nCr = n!/[r!(n-r)!]
#  Here
#  C : Indicates number of combination
#  n : Number of objects in set
#  r : Subset size
p = 1
k = 1
if (n - r < r)
#  Change r when (n - r) is smaller to r
r = n - r
end

if (r != 0)
while (r >= 1)
p = p * n
k = k * r
#  Reduce the value of  n and r by 1
n -= 1
r -= 1
#  Get gcd of p and k
d = self.gcd(p, k)
#  Divide p and k by d
#  d is common divisor
p = p / d
k = k / d
end

else

p = 1
end

#  Display given value
print(" Given n : ", objects ," r : ", subset, "\n")
#  Display calculated result
print(" Result  : ", p, "\n")
end

end

def main()
#  Test Case
end

main()``````

#### input

`````` Given n : 7 r : 2
Result  : 21
Given n : 12 r : 3
Result  : 220
``````
``````/*
Scala program
Calculate value of ncr efficiently
*/
class Combinations()
{
// Returns the gcd value of two numbers
def gcd(a: Long, b: Long): Long = {
if (b == 0)
{
return a;
}
return gcd(b, a % b);
}
def findNcR(objects: Int, subset: Int): Unit = {
var n: Int = objects;
var r: Int = subset;
// Formula of nCr
// nCr = n!/[r!(n-r)!]
// Here
// C : Indicates number of combination
// n : Number of objects in set
// r : Subset size
var p: Long = 1;
var k: Long = 1;
if (n - r < r)
{
// Change r when (n - r) is smaller to r
r = n - r;
}
if (r != 0)
{
while (r >= 1)
{
p = p * n;
k = k * r;
// Reduce the value of  n and r by 1
n -= 1;
r -= 1;
// Get gcd of p and k
var d: Long = gcd(p, k);
// Divide p and k by d
// d is common divisor
p = p / d;
k = k / d;
}
}
else
{
p = 1;
}
// Display given value
println(" Given n : " + objects + " r : " + subset);
// Display calculated result
println(" Result  : " + p);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Combinations = new Combinations();
// Test Case
}
}``````

#### input

`````` Given n : 7 r : 2
Result  : 21
Given n : 12 r : 3
Result  : 220``````
``````/*
Swift 4 program
Calculate value of ncr efficiently
*/
class Combinations
{
// Returns the gcd value of two numbers
func gcd(_ a: Int, _ b: Int) -> Int
{
if (b == 0)
{
return a;
}
return self.gcd(b, a % b);
}
func findNcR(_ objects: Int, _ subset: Int)
{
var n: Int = objects;
var r: Int = subset;
// Formula of nCr
// nCr = n!/[r!(n-r)!]
// Here
// C : Indicates number of combination
// n : Number of objects in set
// r : Subset size
var p: Int = 1;
var k: Int = 1;
if (n - r < r)
{
// Change r when (n - r) is smaller to r
r = n - r;
}
if (r  != 0)
{
while (r >= 1)
{
p = p * n;
k = k * r;
// Reduce the value of  n and r by 1
n -= 1;
r -= 1;
// Get gcd of p and k
let d: Int = self.gcd(p, k);
// Divide p and k by d
// d is common divisor
p = p / d;
k = k / d;
}
}
else
{
p = 1;
}
// Display given value
print(" Given n : ", objects ," r : ", subset);
// Display calculated result
print(" Result  : ", p);
}
}
func main()
{
// Test Case
}
main();``````

#### input

`````` Given n :  7  r :  2
Result  :  21
Given n :  12  r :  3
Result  :  220``````
``````/*
Kotlin program
Calculate value of ncr efficiently
*/
class Combinations
{
// Returns the gcd value of two numbers
fun gcd(a: Long, b: Long): Long
{
if (b == 0L)
{
return a;
}
return this.gcd(b, a % b);
}
fun findNcR(objects: Int, subset: Int): Unit
{
var n: Int = objects;
var r: Int = subset;
// Formula of nCr
// nCr = n!/[r!(n-r)!]
// Here
// C : Indicates number of combination
// n : Number of objects in set
// r : Subset size
var p: Long = 1;
var k: Long = 1;
if (n - r < r)
{
// Change r when (n - r) is smaller to r
r = n - r;
}
if (r != 0)
{
while (r >= 1)
{
p = p * n;
k = k * r;
// Reduce the value of  n and r by 1
n -= 1;
r -= 1;
// Get gcd of p and k
val d: Long = this.gcd(p, k);
// Divide p and k by d
// d is common divisor
p = p / d;
k = k / d;
}
}
else
{
p = 1;
}
// Display given value
println(" Given n : " + objects + " r : " + subset);
// Display calculated result
println(" Result  : " + p);
}
}
fun main(args: Array < String > ): Unit
{
// Test Case
}``````

#### input

`````` Given n : 7 r : 2
Result  : 21
Given n : 12 r : 3
Result  : 220``````
``````package main
import "fmt"
/*
Go program
Calculate value of ncr efficiently
*/

// Returns the gcd value of two numbers
func gcd(a, b int) int {
if b == 0 {
return a
}
return gcd(b, a % b)
}
func findNcR(objects, subset int) {
var n int = objects
var r int = subset
// Formula of nCr
// nCr = n!/[r!(n-r)!]
// Here
// C : Indicates number of combination
// n : Number of objects in set
// r : Subset size
var p int = 1
var k int = 1
if n - r < r {
// Change r when (n - r) is smaller to r
r = n - r
}
if r != 0 {
for (r >= 1) {
p = p * n
k = k * r
// Reduce the value of  n and r by 1
n--
r--
// Get gcd of p and k
var d int =  gcd(p, k)
// Divide p and k by d
// d is common divisor
p = p / d
k = k / d
}
} else {
p = 1
}
// Display given value
fmt.Println(" Given n : ", objects, " r : ", subset)
// Display calculated result
fmt.Println(" Result  : ", p)
}
func main() {
// Test Case
findNcR(7, 2)
findNcR(12, 3)
}``````

#### input

`````` Given n : 7 r : 2
Result  : 21
Given n : 12 r : 3
Result  : 220``````
``````/*
Rust program
Calculate value of ncr efficiently
*/
fn main(){
// Test Case
find_n_c_r(7, 2);
find_n_c_r(12, 3);
}
// Returns the gcd value of two numbers
fn  gcd( a: i32, b: i32) -> i32
{
if b == 0
{
return a;
}
return gcd(b, a % b);
}
fn find_n_c_r( objects: i32, subset: i32)
{
let mut n: i32 = objects;
let mut r: i32 = subset;
// Formula of nCr
// nCr = n!/[r!(n-r)!]
// Here
// C : Indicates number of combination
// n : Number of objects in set
// r : Subset size
let mut p = 1;
let mut k = 1;
if (n - r) < r
{
// Change r when (n - r) is smaller to r
r = n - r;
}
if r != 0
{
while r >= 1
{
p = p * n;
k = k * r;
// Reduce the value of  n and r by 1
n=n-1;
r=r-1;
// Get gcd of p and k
let d =  gcd(p, k);
// Divide p and k by d
// d is common divisor
p = p / d;
k = k / d;
}
}
else
{
p = 1;
}
// Display given value
println!(" Given n : {0} r : {1} ", objects , subset);
// Display calculated result
println!(" Result  : {0}" , p);
}``````

#### input

`````` Given n : 7 r : 2
Result  : 21
Given n : 12 r : 3
Result  : 220``````

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

### New Comment 