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

3.67
3
Leshake 135 points

                                    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 &gt; b) 
        return gcd(a-b, b); 
    return gcd(a, b-a); 
}

3.67 (3 Votes)
0
3.67
3
Katarzyna 120 points

                                    #include &lt;stdio.h&gt;
int main()
{
    int t, n1, n2, gcd; 
    scanf(&quot;%d&quot;, &amp;t); // Test Case Input
    while (t--)
    {
        scanf(&quot;%d %d&quot;, &amp;n1, &amp;n2);// Taking numbers input

        if (n2 &gt; n1)
        {

            gcd = n1;
            n1 = n2;
            n2 = gcd;
        }
        while (n1 % n2 != 0)
        {
            gcd = n2;
            n2 = n1 % n2;
            n1 = gcd; 
        }
        // n2 is our gcd
        printf(&quot;GCD: %d\n&quot;, n2);
    }
    return 0;
}

3.67 (3 Votes)
0
3.8
5
JANK 120 points

                                    #include&lt;iostream&gt;
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&lt;&lt;euclid_gcd(0,7)&lt;&lt;endl;
	cout&lt;&lt;euclid_gcd(55,78)&lt;&lt;endl;
	cout&lt;&lt;euclid_gcd(105,350)&lt;&lt;endl;
	cout&lt;&lt;euclid_gcd(350,105)&lt;&lt;endl;
	return 0;
}

3.8 (5 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
gcd(x,10)=xx program to find a gcd gcd(10,18) gcd(0, 0) gcd cl gcd(5,0) gcd function c++ 14 gcd(899999,1000000) gcd(9,10) gcd(x,y)=gcd(-x,y) gcd code in c c++ program to find gcd gcd in c GCD(n,d)==1. gcd in c++ reference gcd in c program with function gcd(4,8,10) function to calculate gcd in c++ gcd of an array c++ gcd code gcd(5^m, 7^n) gcd function c++ time complexity __gcd function in c++ gcd cpp gfg gcd code in cpp c++ find gcd in o(1) function to find gcd in c++ inbuild gcd function c++ gcd in math gcd c++ code is there any gcd function in c++ c++ gcd function complexity gcd (0,0) is gcd a function in C gcd(x, x + 1) gcd in c++ function gcd(36,84) gcd c++ reference download gcd reference in c gcd function c++ for strings gcd of string c++ gcd(0,3) what is gcd function in c++ gcd(36, 48) gcd(0, 5) finding gcd cpp gcd built in functionc++ gcd stl in cpp get gcd in c++ gcd(a+k,b+k) how to take a gcd of an array in c++\ g++ __gcd gcd in c programming code for gcd in c++ gcd of a vector in c++ gcd c++ header _gcd in c gcd of an array inc ++ gcd c program code to find gcd in c++ gcd function stl cpp gcd methods gcd in c program gcd code cpp c++ find gcd gcd in c gcd(a + x, b + x) gcd with stl c++ gcd function gfg find gcd cpp is gcd is built in c++ gcd(a+b,c+d) gcd implementation cpp gcd implementation c++ built in gcd function c++ gcd(x) gcd c++ stl gcd (a,b) how to find gcd using build in function c++ gcd() c++ gcd function c+++ GCD implementation in c++ #define gcd(a,b) _gcd(a,b) in cpp inbuilt function of gcd in c++ gcd(a+b, a+c) cpp find gcd gcd(k+a,k+b) gcd function in c++ logn gcd(k, k + a) how to use built in gcd in c++ gcd(a+b,a+c) gcd(a+c,b+c) gcd(a,b) gcd function c++ hackerearth gcd function c calculate gcd in cpp gcd example cpp gcd c++ std gcd gcd in stl gcd(a+b,c) gcd cpp code gcd of array c++ gcd inbuilt function in c++ gcd(a gcd(b c)) gcd implementation gcd(3,26) in c++ gcd program in cpp gcd finding algorithm c++ built function for gcd in c++ gcd of array in cpp gcd program in c using function gcd cp algorithms gcd(0,0) gcd function gcd of an array using cpp function for gcd gcd of an array cpp gcd of array in c++ gcd in c++ of n numbers inbuild function for gcd in c++ gcd(n,k) gcd algorithm c++ gcd function c++ header cpp gcd function gcd program in c gcd function in cpp how to add gcd c++ gcd c++ reference GCD function in C what is gcd(a + b, a + c) inbuilt gcd function in c++ is not working recursive function gcd c++ return function gcd c++ __gcd() c++ using inbuilt gcd in c++ gcd algorithm gcd algorithm in c gcd(a b)=gcd(a b-a) gcd(a-m,b-m) gcd formula inbuilt function for gcd in c++ find hcf c++ program hcf in cpp inbuilt hcf in cpp predefined functions of lcm and gcd in c wap to find gcd of two numbers in c++ gcd of 2 numbers in cpp gcd algorithm in c++ header file for gcd psudo code for fhcf of a number using iteration gcd of two numbers c++ stl greatest common divisor algorithm c++ how to find the hcf of two numbers in c++ __gcd() gcd of x numbers in c++ how does stl implement gcd how to find gcd of (a+b) in c++ calculate greatest common divisor c++ highest common factor function c++ gcd function cpp inbuilt function to find gcd in c++ hcf using recursion c++ gcd using stl function to find gcd in c++ stl how to include gcd in cpp how to find gcd of two numbers in c++ __gcd header file ans = (ans * i)/(__gcd(ans, i)); directly find gcd program to find gcd of two numbers in c++ program to find hcf of two numbers in c++ function to find gcd c++ hcf of number cpp __gcd c++ header get gcd function c++ Code and analyze to compute the greatest common divisor (GCD) of two numbers. Code and analyze to compute the greatest common divisor (GCD) of two numbers gcd std c++ gcd c++ function c++ gcd of two numbers find gcd function how to find gcd of 2 numbers c++ greatest common divisor practice problems c++ stl gcd in c++ how to find gcd of numbers in c++ find hcf in cpp GCD function inc++ find gcd of a number in c++ c++ greatest common divisor find gcd in coding logic of gcd in c++ highest common divisor calculator c++ how to find gcf of two numbers c++ how to gcd of two numbers c++ __gcd function c++ second greatest common divisor c++ greatest common divisor c++ non recurssion gdc c++ gcd code c++ gcd in c++ using libraries large gcd code in c++ GCD of two nos c++ greatest common factor c++ shortest method to find gcd c++ how to find gcd c++ gcd code in c++ calaculating gcd c++ c++ program to calculate gcd c++ built in gcd using euclidean algorithm c++ built in gcd hcf in c++ stl hcf inbuilt functions hcf function in c++ how to use gcd c++ compute gcd in c++ gcd of 2 numbers c++ how to find gcd in cpp using stl built in gcd function in c++ inbuilt gcd function c++ __gcd cpp inbuilt function to find gcd c++ gcd built in c++ find gcd c++ find gcd in c++ gcd library c++ gcd stl in c++ gcd of two numbers in cpp gcd program in c++ how to calulate hcf in cpp how to find gcd in c++ gcd stl c++ find gcd in cpp inbuilt function to find gcd cpp gcd of two numbers in c++ stl c++ __gcd whay __gcd and not just gcd cpp math.gcd c++ gcd function c++ stl __gcd in c++ c++ gcd function c++ inbuilt gcd ___gcd cpp c++ gcd gcd function for c++ gcd c++ stl greatest common divisor in c++ c++ greatest common factor _gcd inbult gcd oin c++ inbuilt gcd function in c++ gcd stl c++ builtin gcd gcd of two numbers c++ gcd in cpp gcd of two numbers in c++ gcd c++ gcd template c++ gcd function in c++ stl how to find gcd of two numbers in cpp gcd in c++ stl how to find greatest common divisor in c++ gcd cpp gcd in c++ __gcd c++ gcd function c++ greatest common divisor c++ gcd function in c++
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source