// binomial.cpp // // Abdul Khan // #include #include double choose(const double N, const double x) { double c = 0; for (int i=1;i<=N;i++) { c += log(i); if (i <= x ) c -= log(i); if (i <= N-x) c -= log(i); } return pow(exp(1),c); }; int main(int argc, char* argv[]) { double N,x; double p = 0.5; cout << "# This program will calculate the probability that an N \n"; cout << "# events will result in one of the outcomes exactly x times\n"; cout << "# for 0<=x<=N"; cout << "# Please enter N "; cin >> N; for (x=0;x<=N;x=x+1) cout << x << "\t" << (choose(N,x)*pow(p,x)*(pow((1-p),(N-x)))) << endl; return 0; }