Input
Enter value of n and r : 5 2
Output
Combination : 10
Input
Enter value of n and r : 6 3
Output
Combination : 20
Step 1: START
Step 2: Enter 'n' and 'r'as input.
Step 3: calculate n! ,(n-r)! and r! separately
use formula of combination ncr=n!/(n-r)!*r!
Step 5:print combination .
Step 6: END
/* Program to calculate combination */
#include<stdio.h>
long combination(int n, int r);
long factorial(int num);
int main()
{
int n,r;
printf(" Enter the value of n : ");
scanf("%d",&n);
printf(" Enter the value of r : ");
scanf("%d",&r);
printf(" Combination is : %ld",combination(n,r));
return 0;
}
long permutaion(int n,int r)
{
return factorial(n) / factorial(n-r);
}
long combination(int n,int r)
{
return permutaion(n,r) / factorial(r);
}
long factorial(int num)
{
long long fact =1;
while(num > 0)
{
fact = fact * num;
num--;
}
return fact;
}
/* Program to calculate combination */
#include<iostream>
using namespace std;
long combination(int n, int r);
long factorial(int num);
int main()
{
int n,r;
cout<<" Enter the value of n : ";
cin>>n;
cout<<" Enter the value of r : ";
cin>>r;
cout<<" Combination is : "<<combination(n,r);
return 0;
}
long permutaion(int n,int r)
{
return factorial(n) / factorial(n-r);
}
long combination(int n,int r)
{
return permutaion(n,r) / factorial(r);
}
long factorial(int num)
{
long long fact =1;
while(num > 0)
{
fact = fact * num;
num--;
}
return fact;
}
Enter the value of n : 5
Enter the value of r : 2
Combination is = 10
advertisment