Answers for "gcd of two numbers c++"

C++
3

gcd of two numbers c++

// gcd function definition below:
int gcd(int a, int b) {
   if (b == 0)
   return a;
   return gcd(b, a % b);
}

int a = 105, b = 30;
cout<<"GCD of "<< a <<" and "<< b <<" is "<< gcd(a, b);
// output = "GCD of 105 and 30 is 15";
Posted by: Guest on September-16-2020
2

gcd in c++

#include<iostream>
using namespace std;
long long gcd(long long a, long long b) 
{ 
    if (b == 0) 
        return a; 
    return gcd(b, a % b);  
      
} 
int main()
{
	long long a,b;
	cin>>a>>b;
	cout<<gcd(a,b);
}
Posted by: Guest on September-19-2020
5

gcd function in c++

int gcd(int a, int b) 
{ 
    // Everything divides 0  
    if (a == 0) 
       return b; 
    if (b == 0) 
       return a; 
    // base case 
    if (a == b) 
        return a; 
    // a is greater 
    if (a > b) 
        return gcd(a-b, b); 
    return gcd(a, b-a); 
}
Posted by: Guest on June-08-2020

Code answers related to "gcd of two numbers c++"

Browse Popular Code Answers by Language