Problem Description:
Item 2-factorial of some numbers no, forget it.
The n! function, when the user's input is negative, and the input number is too large (for example, greater than 12), use the exception handling mechanism to reject, and give appropriate hints.
Code implementation:
#include <iostream> #include <cstdio>using namespace Std;int main () { int n; try{ printf ("Please enter a number:"); scanf ("%d", &n); if (n<0) throw 1; if (n>12) throw 2; int sum=1,i; for (i=n;i>0;--i) sum*=i; cout<<n<< "The factorial is" <<sum<< ' \12 '; } catch (int t) { if (t==1) { printf ("Input error, please enter a number greater than 0!\n"); } if (t==2) { printf ("Input error, please enter a number not greater than 12!\n"); } } return 0;}
Operation Result:
The 16th Week "C + + Language foundation" practice reference--some number of factorial not forget