#include <iostream>
#include <cmath>
usingnamespace std;
double Bernoulli( int n, double p, int k )
{
double log_answer = k * log( p ) + ( n - k ) * log( 1.0 - p );
for ( int nn = n, kk = k; kk; nn--, kk-- ) log_answer += log( (double)nn / kk );
return exp( log_answer );
}
int main()
{
int n, kmax;
double p;
cout << "Input the number of trials (n): "; cin >> n;
cout << "Input the probability of success on each trial (p): "; cin >> p;
cout << "Input the MAXIMUM number of successes (kmax): "; cin >> kmax;
double cumulative_probability = 0;
for ( int k = 0; k <= kmax; k++ ) cumulative_probability += Bernoulli( n, p, k );
cout << "P( result <= " << kmax << " ) is " << cumulative_probability << '\n';
}
Input the number of trials (n): 100
Input the probability of success on each trial (p): 0.3
Input the MAXIMUM number of successes (kmax): 35
P( result <= 35 ) is 0.883921