
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- 8086 program to generate AP series of n numbers
- Java Program to create an array with randomly shuffled numbers in a given range
- 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 Largest Number Among Three Numbers
- Python program to find the maximum of three numbers
- C# program to find the maximum of three numbers
- Java Program to Find the Largest Among Three Numbers
- Swift Program to Find the Largest Among Three Numbers
- Haskell program to find the largest among three numbers
- Kotlin Program to Find the Largest Among Three Numbers
- PyTorch – How to invert the colors of an image randomly with a given probability?
- 8085 program to add three 16 bit numbers stored in registers

Advertisements