Answers for "gcd function c++"

C++
2

gcd function c++

ll gcd(ll a, ll b)
{
    if (b==0)return a;
    return gcd(b, a % b);   
}
Posted by: Guest on September-19-2021
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
1

gcd in c++

#include<iostream>
using namespace std;

int euclid_gcd(int a, int b) {
	if(a==0 || b==0) return 0;
	int dividend = a;
	int divisor = b;
	while(divisor != 0){
		int remainder = dividend%divisor;
		dividend = divisor;
		divisor = remainder;
	}
	return dividend;
}

int main()
{
	cout<<euclid_gcd(0,7)<<endl;
	cout<<euclid_gcd(55,78)<<endl;
	cout<<euclid_gcd(105,350)<<endl;
	cout<<euclid_gcd(350,105)<<endl;
	return 0;
}
Posted by: Guest on September-22-2020
0

gcd using cpp

#include <bits/stdc++.h>
using namespace std;

int gcd(int a,int b){
    if(a==0){
        return b;
    }
    if(b==0){
        return a;
    }

    if(a==b){
        return a;
    }

    int x=0;
    int mx = INT_MIN;
    int mi = min(a,b);
    for(int i=2;i<=mi;i++){
        if(a%i==0 and b%i==0){
            mx = max(mx,i);
        }
    }

    return mx;

}

int main() {
    int a = 36;
    int b = 60;
    cout<<gcd(a,b);

}
Posted by: Guest on August-29-2021

Browse Popular Code Answers by Language