vendredi 20 septembre 2019

Permutation and Combination c++ logical error

This is my assignment, i finished writing my code and it does compile but it is not giving the right answer. So i know there is some logical error but i cannot find out what.

Plz check and tell me, simple code to calculate permutations and combinations

#include <iostream>

using namespace std;

int factorial(int n)
{
    if (n == 0 or n == 1)
    {
        return 1;
    }
    else
    {
        return n * factorial(n - 1);
    }
}
int permutation(int a, int b)
{
    //factorial(a);

    int perm = factorial(a) / factorial(a - b);

    return perm;
}
int combination(int a, int b)
{
    int permutation(int a, int b);
    int factorial(int n);

    return permutation(a, b) / factorial(b);;
}

int main()
{
    int n;
    int r;

    cout << "Enter n: " << endl;
    cin >> n;
    cout << "Enter r: " << endl;
    cin >> r;



    int factorial(int n);
    int permutation(int n, int r);
    int combination(int n, int r);

    if (n >= r)
    {

        cout << "Permutuation: " << permutation << endl;
        cout << "Combination: " << combination << endl;

    }
    else
    {
        cout << "Invalid" << endl;
    }
    return 0;
}

The answer given by the console

Enter n:
6
Enter r:
5
Permutuation: 00AC1375
Combination: 00AC133E

Aucun commentaire:

Enregistrer un commentaire