VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-find-gcd-floating-point-numbers/

⇱ Program to find GCD of floating point numbers - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to find GCD of floating point numbers

Last Updated : 12 Feb, 2025

The greatest common divisor (GCD) of two or more numbers, which are not all zero, is the largest positive number that divides each of the numbers. 
Example:

Input : 0.3, 0.9
Output : 0.3
Explanation: The GCD of 0.3 and 0.9 is 0.3 because both numbers share 0.3 as the largest common divisor.

Input : 0.48, 0.108
Output : 0.012
Explanation: The GCD of 0.48 and 0.108 is 0.012 because 0.012 is the largest value that divides both numbers exactly.

The approach to solve this problem is to expressing each of the numbers without decimals as the product of primes we get:

For Example
a = 1.20,b = 22.5
120
2250

Now, GCD of 120 and 2250 = 2*3*5=30 
Therefore, the H.C.F. of 1.20 and 22.5 = 0.30 (taking 2 decimal places)

We can find GCD using the Euclidean algorithm. This algorithm indicates that if the smaller number is subtracted from a bigger number, GCD of two numbers doesn’t change.  


Output
0.3


Comment
Article Tags: