TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Program to print the combination (nCr) of the given number

Program to print the combination (nCr) of the given number on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph, pattern, string etc.

<< Back to PROGRAM

Program to print the combination (nCr) of the given number

Combination (nCr) can be defined as the combination of n things taken r at a time without any repetition.

ncr can be calculated as,

nCr = [n(n-1) ... (n-r+1)] / r(r-1)...1

Algorithm

MAIN

  • STEP 1: START
  • STEP 2: DEFINE n, r
  • STEP 3: ENTER n, r
  • STEP 4: PRINT nCr(n, r)
  • STEP 5: END

nCr(n r)

  • STEP 1: START
  • STEP 2: RETURN fact(n) / (fact(r)*fact (n-r))
  • STEP 3: END

fact(n)

  • STEP 1: START
  • STEP 2: SET res= 1
  • STEP 3: REPEAT STEP 3 and STEP 4 UNTIL i<=n
  • STEP 4: res = res*i
  • STEP 5: RETURN res
  • STEP 6: END

Java Program

import java.util.*;
class Combination { 
static int nCr(int n, int r) 
{ 
	return fact(n) / (fact(r) * 
	fact(n - r)); 
} 
static int fact(int n) 
{ 
	int res = 1; 
           for (int i = 2; i <= n; i++) 
           res = res * i; 
          return res; 
} 
public static void main(String[] args) 
{ 
int n,r;
System.out.println("Enter the value of n and r?");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
r = sc.nextInt();
System.out.println("nCr = "+nCr(n, r));
} 
}

Output:

Enter the value of n and r?
6
4
nCr = 15

C Program

#include <stdio.h>
int fact(int z);
void main()
{
    int n, r, nCr;
    printf("Enter the value of n and r?");
    scanf("%d %d",&n,&r);
    nCr = fact(n) / (fact(r) * fact(n - r));
    printf("\nnCr = %d", nCr);
}

int fact(int z)
{
    int f = 1, i;
    if (z == 0)
    {
        return(f);
    }
    else
    {
        for (i = 1; i <= z; i++)
	{
            f = f * i;
	}
    }
    return(f);
}

Output:

Enter the value of n and r? 5  3
nCr = 10

Python Program

def fact(z):
    f = 1
    if z == 0:
        return f;
    else:
        for i in range(1,z+1):
            f = f * i;
        return f;


n=int(input("Enter the value of n"))
r = int(input("Enter the value of r"))
nCr = fact(n) / (fact(r) * fact(n - r));
print("\nnCr = %d" %(nCr));

Output:

Enter the value of n 5
Enter the value of r 3
nCr = 10

C# Program

using System; 
class Combination
{
static int nCr(int n, int r) 
{ 
   return fact(n) / (fact(r) * 
     fact(n - r)); 
} 
  static int fact(int n) 
{ 
  int res = 1; 
  for (int i = 2; i <= n; i++) 
   res = res * i; 
  return res; 
} 
   public static void Main() 
 { 
   int n,r;
   Console.WriteLine("Enter the value of n and r?");
   n = Convert.ToInt32(Console.ReadLine());
   r = Convert.ToInt32(Console.ReadLine());
   Console.Write("nCr = "+nCr(n, r)); 
 } 
} 

Output:

Enter the value of n and r? 5 3  
nCr = 10

PHP Program

<?php 
function nCr( $n, $r) 
{ 
  return fact($n) / (fact($r) *  fact($n - $r)); 
} 
function fact( $n) 
{ 
 $res = 1; 
  for ( $i = 2; $i <= $n; $i++) 
  $res = $res * $i; 
  return $res; 
} 
 echo "Enter the value of n and r?";
 $n = readline(); 
 $r = readline(); 
  echo "ncr = ";
  echo nCr($n, $r); 
?> 

Output:

Enter the value of n and r? 5 3  
nCr = 10
Next Topic#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf