Probability of getting a perfect square when a random number is chosen in a given range | C ++ implementation to find the probability of getting a perfect square number ; Function to return the probability of getting a perfect square number in a range ; Count of perfect squares ; Total numbers in range l to r ; Calculating probability ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; float findProb ( int l , int r ) { float countOfPS = floor ( sqrt ( r ) ) - ceil ( sqrt ( l ) ) + 1 ; float total = r - l + 1 ; float prob = ( float ) countOfPS / ( float ) total ; return prob ; } int main ( ) { int L = 16 , R = 25 ; cout << findProb ( L , R ) ; return 0 ; }