- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# C++ Program Probability for three randomly chosen numbers to be in AP

Given with an array of numbers ‘n’ and the task is to find the probability of three randomly chosen numbers to be in AP.

## Example

Input-: arr[] = { 2,3,4,7,1,2,3 } Output-: Probability of three random numbers being in A.P is: 0.107692 Input-:arr[] = { 1, 2, 3, 4, 5 } Output-: Probability of three random numbers being in A.P is: 0.151515

**Approach used in the below program is as follows** −

- Input the array of positive integers
- Calculate the size of the array
Apply the formula given below to find the probability of three random numbers to be in AP

3 n / (4 (n * n) – 1)

- Print the result

## ALGORITHM

Start Step 1-> function to calculate the probability of three random numbers be in AP double probab(int n) return (3.0 * n) / (4.0 * (n * n) - 1) Step 2->In main() declare an array of elements as int arr[] = { 2,3,4,7,1,2,3 } calculate size of an array as int size = sizeof(arr)/sizeof(arr[0]) call the function to calculate probability as probab(size) Stop

## Example

#include <bits/stdc++.h> using namespace std; //calculate probability of three random numbers be in AP double probab(int n) { return (3.0 * n) / (4.0 * (n * n) - 1); } int main() { int arr[] = { 2,3,4,7,1,2,3 }; int size = sizeof(arr)/sizeof(arr[0]); cout<<"probability of three random numbers being in A.P is : "<<probab(size); return 0; }

## Output

Probability of three random numbers being in A.P is: 0.107692

- Related Questions & Answers
- 8086 program to generate AP series of n numbers
- C++ Program to Generate Random Numbers Using Probability Distribution Function
- Java program to Largest number among three numbers
- Java program to find maximum of three numbers
- Program to find largest of three numbers - JavaScript
- C# program to find the maximum of three numbers
- C++ Program to Find Largest Number Among Three Numbers
- Python program to find the maximum of three numbers
- Java Program to Find the Largest Among Three Numbers
- PyTorch – How to invert the colors of an image randomly with a given probability?
- Java Program to create an array with randomly shuffled numbers in a given range
- C program to Find the Largest Number Among Three Numbers
- 8085 program to add three 16 bit numbers stored in registers
- Program to find the common ratio of three numbers in C++
- Subjective Probability Vs. Objective Probability

Advertisements