Print the Probability
In this step, we'll enhance our binomial probability program to provide more detailed and formatted output of the probability calculation.
We'll modify the previous program to include additional probability representations:
#include <stdio.h>
#include <math.h>
// Previous functions (factorial, combinations, binomial_probability) remain the same
int main() {
int n, k;
double p;
printf("Enter number of trials (n): ");
scanf("%d", &n);
printf("Enter probability of success (p): ");
scanf("%lf", &p);
printf("Enter number of successes (k): ");
scanf("%d", &k);
double prob = binomial_probability(n, k, p);
// Detailed probability output
printf("\nProbability Calculation Results:\n");
printf("-----------------------------\n");
printf("Number of Trials (n): %d\n", n);
printf("Probability of Success (p): %.4f\n", p);
printf("Number of Successes (k): %d\n", k);
// Different probability representations
printf("\nProbability Representations:\n");
printf("Decimal: %f\n", prob);
printf("Percentage: %.2f%%\n", prob * 100);
printf("Fraction: 1 in %.0f\n", 1.0 / prob);
return 0;
}
Compile and run the program:
gcc ~/project/binomial_prob.c -o ~/project/binomial_prob -lm
~/project/binomial_prob
Example output:
Enter number of trials (n): 10
Enter probability of success (p): 0.5
Enter number of successes (k): 6
Probability Calculation Results:
-----------------------------
Number of Trials (n): 10
Probability of Success (p): 0.5000
Number of Successes (k): 6
Probability Representations:
Decimal: 0.205078
Percentage: 20.51%
Fraction: 1 in 5
Key improvements in this step:
- Added detailed result formatting
- Displayed multiple probability representations
- Improved readability of output