Check out example codes for "gcd in c++". It will help you in understanding the concepts better.

Code Example 1

#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;
}

Code Example 2

// CPP program to illustrate 
// gcd function of C++ STL 
#include <iostream> 
#include <algorithm>  
  
using namespace std; 
  
int main() 
{ 
    cout << "gcd(6, 20) = " << __gcd(6, 20) << endl; 
}

Code Example 3

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

Code Example 4

#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);
}

Code Example 5

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); 
}

Learn ReactJs, React Native from akashmittal.com