Combination Number Generator || Combination series || C++
In this, we are going see a program on combination number generator using Combination series : C(n,r)=n!/r!(n-r)! in C++ Programming Language.


The Code given below can be used in TURBO C++ Compilers: -

#include <iostream.h>
#include<conio.h>
#include <stdio.h>
#include <string.h>

class Fact
{
public:
    int factorial(int num)
    {
        if (num > 1)
        {
            return num * factorial(num - 1);
        }
        else
        {
            return 1;
        }
    }
};

int main()
{
    clrscr();
    char str[50];
    Fact obj;
    int r;
    cout << "Enter a string to calculate the number of combinations possible for it:\n";
    gets(str);
    int n = strlen(str);
    cout << "Enter the number of items taken for calculations: ";
    cin >> r;
    float P = (float)obj.factorial(n) / obj.factorial(n - r);
    P = P / obj.factorial(r);
    cout << "Number of Combinations of given string with " << r << " permuting items is = " << P;
    getch();
    return 0;
}

The Code given below can be used in gcc/g++ Compilers: -

#include <iostream>
#include <string.h>
using namespace std;

class Fact
{
public:
    int factorial(int num)
    {
        if (num > 1)
        {
            return num * factorial(num - 1);
        }
        else
        {
            return 1;
        }
    }
};

int main()
{
    char str[50];
    Fact obj;
    int r;
    cout << "Enter a string to calculate the number of combinations possible for it:\n";
    gets(str);
    int n = strlen(str);
    cout << "Enter the number of items taken for permutations: ";
    cin >> r;
    float P = (float)obj.factorial(n) / obj.factorial(n - r);
    P = P / obj.factorial(r);
    cout << "Number of Combinations of given string with " << r << " permuting items is = " << P;
    return 0;
}

#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post