gcd code example

Example 1: gcd

int gcd(int a, int b) {
    while (b) b ^= a ^= b ^= a %= b;
    return a;
}

Example 2: gcd

import math

a = 10
b = 8
answer = math.gcd(10, 8)
print(answer)

Example 3: gcd

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

Example 4: gcd

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

Example 5: gcd

//using euclid's theorem to find gcd
#include <iostream>

using namespace std;
int gcd(int x,int y)
{
    if(y==0)
    {
        return x;
    }
    return gcd(y,x%y);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    while(t--)
    {
        int a,b;
        cin>>a>>b;
        int ans=gcd(a,b);
        cout<<ans<<endl;
    }
    return 0;
}

Example 6: gcd

int gcd(int a,int b) {
	while (a&&b) a>b?a%=b:b%=a;
	return a+b;
}