Permutations of a String || Permutation series ||  C++
In this, we are going see a program on Permutation of a String using  Permutation series : P(n,r)= n!/(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;
        }
    }
};

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

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 permutations 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);
    cout << "Number of Permutations 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