Program to find GCD of floating point numbers in C++


In this tutorial, we will be discussing a program to find GCD of floating point numbers.

For this we will be provided with two integers. Our task is to find the GCD (Greatest common divisor) of those two provided integers.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
//returning GCD of given numbers
double gcd(double a, double b){
   if (a < b)
      return gcd(b, a);
   if (fabs(b) < 0.001)
      return a;
   else
      return (gcd(b, a - floor(a / b) * b));
}
int main(){
   double a = 1.20, b = 22.5;
   cout << gcd(a, b);
   return 0;
}

Output

0.3

Updated on: 09-Sep-2020

303 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements