# Program to find GCD of floating point numbers

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 Input : 0.48, 0.108 Output : 0.012

The simplest approach to solve this problem is :

**a=1.20**

**b=22.5**

Expressing each of the numbers without decimals as the product of primes we get:

**120**

**2250**

Now, H.C.F. 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 do this 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.

## C++

`// CPP code for finding the GCD of two floating ` `// numbers. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Recursive function to return gcd of a and b ` `double` `gcd(` `double` `a, ` `double` `b) ` `{ ` ` ` `if` `(a < b) ` ` ` `return` `gcd(b, a); ` ` ` ` ` `// base case ` ` ` `if` `(` `fabs` `(b) < 0.001) ` ` ` `return` `a; ` ` ` ` ` `else` ` ` `return` `(gcd(b, a - ` `floor` `(a / b) * b)); ` `} ` ` ` `// Driver Function. ` `int` `main() ` `{ ` ` ` `double` `a = 1.20, b = 22.5; ` ` ` `cout << gcd(a, b); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// JAVA code for finding the GCD of ` `// two floating numbers. ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Recursive function to return gcd ` ` ` `// of a and b ` ` ` `static` `double` `gcd(` `double` `a, ` `double` `b) ` ` ` `{ ` ` ` `if` `(a < b) ` ` ` `return` `gcd(b, a); ` ` ` ` ` `// base case ` ` ` `if` `(Math.abs(b) < ` `0.001` `) ` ` ` `return` `a; ` ` ` ` ` `else` ` ` `return` `(gcd(b, a - ` ` ` `Math.floor(a / b) * b)); ` ` ` `} ` ` ` ` ` `// Driver Function. ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `double` `a = ` `1.20` `, b = ` `22.5` `; ` ` ` `System.out.printf(` `"%.1f"` `,gcd(a, b)); ` ` ` `} ` `} ` ` ` `/*This code is contributed by Nikita Tiwari.*/` |

*chevron_right*

*filter_none*

## Python

`# Python code for finding the GCD of ` `# two floating numbers. ` ` ` `import` `math ` ` ` `# Recursive function to return gcd ` `# of a and b ` `def` `gcd(a,b) : ` ` ` `if` `(a < b) : ` ` ` `return` `gcd(b, a) ` ` ` ` ` `# base case ` ` ` `if` `(` `abs` `(b) < ` `0.001` `) : ` ` ` `return` `a ` ` ` `else` `: ` ` ` `return` `(gcd(b, a ` `-` `math.floor(a ` `/` `b) ` `*` `b)) ` ` ` ` ` `# Driver Function. ` `a ` `=` `1.20` `b ` `=` `22.5` `print` `(` `'{0:.1f}'` `.` `format` `(gcd(a, b))) ` ` ` `# This code is contributed by Nikita Tiwari. ` |

*chevron_right*

*filter_none*

## C#

`// C# code for finding the GCD of ` `// two floating numbers. ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Recursive function to return gcd ` ` ` `// of a and b ` ` ` `static` `float` `gcd(` `double` `a, ` `double` `b) ` ` ` `{ ` ` ` `if` `(a < b) ` ` ` `return` `gcd(b, a); ` ` ` ` ` `// base case ` ` ` `if` `(Math.Abs(b) < 0.001) ` ` ` `return` `(` `float` `)a; ` ` ` ` ` `else` ` ` `return` `(` `float` `)(gcd(b, a - ` ` ` `Math.Floor(a / b) * b)); ` ` ` `} ` ` ` ` ` `// Driver Function. ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `double` `a = 1.20, b = 22.5; ` ` ` ` ` `Console.WriteLine(gcd(a, b)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP code for finding the GCD ` `// of two floating numbers. ` ` ` `// Recursive function to ` `// return gcd of a and b ` `function` `gcd(` `$a` `, ` `$b` `) ` `{ ` ` ` `if` `(` `$a` `< ` `$b` `) ` ` ` `return` `gcd(` `$b` `, ` `$a` `); ` ` ` ` ` `// base case ` ` ` `if` `(` `abs` `(` `$b` `) < 0.001) ` ` ` `return` `$a` `; ` ` ` ` ` `else` ` ` `return` `(gcd(` `$b` `, ` `$a` `- ` ` ` `floor` `(` `$a` `/ ` `$b` `) * ` `$b` `)); ` `} ` ` ` `// Driver Code ` `$a` `= 1.20; ` `$b` `= 22.5; ` `echo` `gcd(` `$a` `, ` `$b` `); ` ` ` `// This code is contributed ` `// by aj_36 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

0.3

This article is contributed by **Abhishek Sharma**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Check whether given floating point number is even or odd
- Smallest number to multiply to convert floating point to natural
- Fast method to calculate inverse square root of a floating point number in IEEE 754 format
- Program to find the Break Even Point
- Program to find LCM of 2 numbers without using GCD
- Program to find GCD or HCF of two numbers
- Program to find LCM of two numbers
- Program to find LCM of two Fibonnaci Numbers
- Program to find first N Fermat Numbers
- Program to find sum of first n natural numbers
- Program to find sum of prime numbers between 1 to n
- Program to find first N Iccanobif Numbers
- Program to find the common ratio of three numbers
- Program to find the percentage of difference between two numbers
- Program to find the maximum difference between the index of any two different numbers