#3
The following part of a program computes and returns the product 1 * 2 * 3 * … * N where N is the input parameter. (This value is called N! And is much used in combinatorics.) However, this function can give unexpected answers for some inputs of N! (Hint: for negative values of N). Add exception handling to this function to catch this situation. You should include both catch and try blocks. Your exception can be of any type, preferably std::exception.
int productOfNumbers (int N) {
int p = 1;
while (N != 0) { // count down until zero
p *= N;
N–;
}
return p;
}
int main() {
int n;
cout << “give number to compute factorial: “;
cin >> n;
cout << “n! = ” << productOfNumbers(n) << endl;
return 0;
}