Search code examples
factorial

Punctuation Error in factorial function?


I am getting the following error: Expected ";" at end of declaration I get this in the line: int factorial (int q) {

ideas?

Thanks,

Steve

#include <iostream>
using namespace std;


int n=0;
int factorial(int q);
int combination(int i, int j);


int main() {
    cout <<"Enter n "<<endl;
    cin >>n;
    for (int i = 0; i <= n-1; i++) {
        for (int j = 0; j<=i; j++) {
            cout << combination (i, j) << endl;
        }
    }
    return 0;
}

int combination(int i, int j) {
    return factorial(i) / (factorial(j)*factorial(i-j));
    {


int factorial (int q) {
    int result = 1;
    if (q == 0) {
        result = 1;
    } else {
        for (int l=1; l <= q; l++) {
            result *=l;
        }

    }
       return result;
}

This will not compile because of the error.


Solution

  • Your combination function above is not closed properly - you mistyped the closing bracket. It should be:

    int combination(int i, int j) {
        return factorial(i) / (factorial(j)*factorial(i-j));
    }