Program to find GCD of floating point numbers Last Updated : 12 Feb, 2025 Comments Improve Suggest changes Like Article Like Report 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.9Output : 0.3Explanation: 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.108Output : 0.012Explanation: 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 Examplea = 1.20, b = 22.5 120 = 2^3*3*5 2250 = 2*3^2*5^3 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. 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; } 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)); } } 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))) 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)); } } JavaScript <script> // javascript 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 (Math.abs(b) < 0.001) return a; else return (gcd(b, a - Math.floor(a / b) * b)); } // Driver Function. var a = 1.20, b = 22.5; document.write( gcd(a, b).toFixed(1)); // This code is contributed by aashish1995 </script> 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); ?> Output0.3 Comment More infoAdvertise with us Next Article Largest Subset with GCD 1 A Aarti_Rathi and Abhishek Sharma Improve Article Tags : Misc Mathematical DSA GCD-LCM Practice Tags : MathematicalMisc Similar Reads LCM of given array elements In this article, we will learn how to find the LCM of given array elements.Given an array of n numbers, find the LCM of it. Example:Input : {1, 2, 8, 3}Output : 24LCM of 1, 2, 8 and 3 is 24Input : {2, 7, 3, 9, 4}Output : 252Table of Content[Naive Approach] Iterative LCM Calculation - O(n * log(min(a 14 min read GCD of more than two (or array) numbers Given an array arr[] of non-negative numbers, the task is to find GCD of all the array elements. In a previous post we find GCD of two number.Examples:Input: arr[] = [1, 2, 3]Output: 1Input: arr[] = [2, 4, 6, 8]Output: 2Using Recursive GCDThe GCD of three or more numbers equals the product of the pr 11 min read Euler's Totient Function Given an integer n, find the value of Euler's Totient Function, denoted as Φ(n). The function Φ(n) represents the count of positive integers less than or equal to n that are relatively prime to n. Euler's Totient function Φ(n) for an input n is the count of numbers in {1, 2, 3, ..., n-1} that are re 10 min read Euclidean algorithms (Basic and Extended) The Euclidean algorithm is a way to find the greatest common divisor of two positive integers. GCD of two numbers is the largest number that divides both of them. A simple way to find GCD is to factorize both numbers and multiply common prime factors.Examples:input: a = 12, b = 20Output: 4Explanatio 9 min read Stein's Algorithm for finding GCD Stein's algorithm or binary GCD algorithm is an algorithm that computes the greatest common divisor of two non-negative integers. Steinâs algorithm replaces division with arithmetic shifts, comparisons, and subtraction.Examples: Input: a = 17, b = 34 Output : 17Input: a = 50, b = 49Output: 1Algorith 14 min read Program to find LCM of two numbers Given two positive integers a and b. Find the Least Common Multiple (LCM) of a and b.LCM of two numbers is the smallest number which can be divided by both numbers. Input : a = 10, b = 5Output : 10Explanation : 10 is the smallest number divisible by both 10 and 5Input : a = 5, b = 11Output : 55Expla 5 min read GCD, LCM and Distributive Property Given three integers x, y, z, the task is to compute the value of GCD(LCM(x,y), LCM(x,z)) where, GCD = Greatest Common Divisor, LCM = Least Common MultipleExamples: Input: x = 15, y = 20, z = 100Output: 60Explanation: The GCD of 15 and 20 is 5, and the LCM of 15 and 20 is 60, which is then multiplie 4 min read Euler's Totient function for all numbers smaller than or equal to n Euler's Totient function ?(n) for an input n is the count of numbers in {1, 2, 3, ..., n} that are relatively prime to n, i.e., the numbers whose GCD (Greatest Common Divisor) with n is 1. For example, ?(4) = 2, ?(3) = 2 and ?(5) = 4. There are 2 numbers smaller or equal to 4 that are relatively pri 13 min read Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B Given a number n, we need to find the number of ordered pairs of a and b such gcd(a, b) is b itselfExamples : Input : n = 2Output : 3The pairs are (1, 1) (2, 2) and (2, 1) Input : n = 3Output : 5(1, 1) (2, 2) (3, 3) (2, 1) and (3, 1)[Naive Approach] Counting GCD Pairs by Divisor Propertygcd(a, b) = 6 min read Problems on GCDSeries with largest GCD and sum equals to nGiven integers n and m, construct a strictly increasing sequence of m positive integers with sum exactly equal to n such that the GCD of the sequence is maximized. If multiple sequences have the same maximum GCD, return the lexicographically smallest one. If not possible, return -1.Examples : Input 7 min read Program to find GCD of floating point numbersThe 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.9Output : 0.3Explanation: 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 4 min read Largest Subset with GCD 1Given n integers, we need to find size of the largest subset with GCD equal to 1. Input Constraint : n <= 10^5, A[i] <= 10^5Examples: Input : A = {2, 3, 5}Output : 3Explanation: The largest subset with a GCD greater than 1 is {2, 3, 5}, and the GCD of all the elements in the subset is 3.Input 6 min read Summation of GCD of all the pairs up to nGiven a number n, find sum of all GCDs that can be formed by selecting all the pairs from 1 to n. Examples: Input : n = 4Output : 7Explanation: Numbers from 1 to 4 are: 1, 2, 3, 4Result = gcd(1,2) + gcd(1,3) + gcd(1,4) + gcd(2,3) + gcd(2,4) + gcd(3,4) = 1 + 1 + 1 + 1 + 2 + 1 = 7Input : n = 12Output 10 min read GCD of more than two (or array) numbersGiven an array arr[] of non-negative numbers, the task is to find GCD of all the array elements. In a previous post we find GCD of two number.Examples:Input: arr[] = [1, 2, 3]Output: 1Input: arr[] = [2, 4, 6, 8]Output: 2Using Recursive GCDThe GCD of three or more numbers equals the product of the pr 11 min read GCD of digits of a given numberGiven a number n, find GCD of its digits. Examples : Input : 345 Output : 1 GCD of 3, 4 and 5 is 1. Input : 2448 Output : 2 GCD of 2, 4, 4 and 8 is 2 We traverse the digits of number one by one using below loop digit = n mod 10; n = n / 10; While traversing digits, we keep track of current GCD and k 4 min read Maximize GCD of all possible pairs from 1 to NGiven an integer N (> 2), the task is to find the maximum GCD among all pairs possible by the integers in the range [1, N]. Example: Input: N = 5 Output: 2 Explanation : GCD(1, 2) : 1 GCD(1, 3) : 1 GCD(1, 4) : 1 GCD(1, 5) : 1 GCD(2, 3) : 1 GCD(2, 4) : 2 GCD(2, 5) : 1 GCD(3, 4) : 1 GCD(3, 5) : 1 G 3 min read GCD of two numbers formed by n repeating x and y timesGiven three positive integer n, x, y. The task is to print Greatest Common Divisor of numbers formed by n repeating x times and number formed by n repeating y times. 0 <= n, x, y <= 1000000000.Examples : Input : n = 123, x = 2, y = 3. Output : 123 Number formed are 123123 and 123123123. Greate 6 min read Program to find Greatest Common Divisor (GCD) of N stringsGiven an array of string arr[], the task is the Greatest Common Divisor of the given array of string. In strings 'A' and 'B', we say "B divides A" if and only if A = concatenation of B more than 1 times.Find the largest string which divides both A and B. Examples: Input: arr[] = { "GFGGFG", "GFGGFG" 13 min read Find the GCD that lies in given rangeGiven two positive integer a and b and a range [low, high]. The task is to find the greatest common divisor of a and b which lie in the given range. If no divisor exist in the range, print -1.Examples: Input : a = 9, b = 27, low = 1, high = 5 Output : 3 3 is the highest number that lies in range [1, 7 min read Find the maximum GCD of the siblings of a Binary TreeGiven a 2d-array arr[][] which represents the nodes of a Binary tree, the task is to find the maximum GCD of the siblings of this tree without actually constructing it. Example:  Input: arr[][] = {{4, 5}, {4, 2}, {2, 3}, {2, 1}, {3, 6}, {3, 12}} Output: 6 Explanation:  For the above tree, the maxi 11 min read Like