gcd using cpp

Solutions on MaxInterview for gcd using cpp by the best coders in the world

showing results for - "gcd using cpp"
Claudia
30 Sep 2020
1int gcd(int a, int b) 
2{ 
3    // Everything divides 0  
4    if (a == 0) 
5       return b; 
6    if (b == 0) 
7       return a; 
8    // base case 
9    if (a == b) 
10        return a; 
11    // a is greater 
12    if (a > b) 
13        return gcd(a-b, b); 
14    return gcd(a, b-a); 
15}
Anson
04 Feb 2016
1#include <bits/stdc++.h>
2using namespace std;
3
4int gcd(int a,int b){
5    if(a==0){
6        return b;
7    }
8    if(b==0){
9        return a;
10    }
11
12    if(a==b){
13        return a;
14    }
15
16    int x=0;
17    int mx = INT_MIN;
18    int mi = min(a,b);
19    for(int i=2;i<=mi;i++){
20        if(a%i==0 and b%i==0){
21            mx = max(mx,i);
22        }
23    }
24
25    return mx;
26
27}
28
29int main() {
30    int a = 36;
31    int b = 60;
32    cout<<gcd(a,b);
33
34}
35
queries leading to this page
gcd 28a 2cb 2cc 29 c 2b 2bgreatest common factor c 2b 2bhow to find greatest common divisor in c 2b 2blogic of gcd in c 2b 2bgcd methodscode for gcd in c 2b 2bc 2b 2b builtin gcdgcd program in c using functiongcd program in c 2b 2b gcd c 2b 2bgcd function inc 2b 2bgcd in c 2b 2b functionc 2b 2b built in gcd using euclidean algorithminbuilt gcd function in c 2b 2bgcd code c 2b 2bc 2b 2b inbuilt gcdgcd stl in c 2b 2bgcd 28a 2b x 2c b 2b x 29c 2b 2b gcd function complexityhcf inbuilt functionsgcd in c 2b 2b using for loopgcd c 2b 2bhcf in c 2b 2b stlc 2b 2b greatest common factorgcd function c 2b 2b 14gcd stlgcd of two numbers c 2b 2bc 2b 2b gcd functionwhay gcd and not just gcd cppis gcd is built in c 2b 2bmath gcd c 2b 2bgcd in c programminghow to find gcd in c 2b 2bc 2b 2b gcdhow to calulate hcf in cppgcd examplegcd function cfunction for gcd gcd cpp gcd in cppgcd program in cinbuilt function to find gcd in c 2b 2b gcdinbuilt gcd function c 2b 2bgcd functioncompute gcd in c 2b 2bgcd c 2b 2binbuilt gcd function in c 2b 2b is not workinghow to find gcd in cpp using stlgcd 28a 2bk 2cb 2bk 29what to include to use the function gcd in c 2b 2bgcd in cfunction to calculate gcd in c 2b 2bgcd function gfggcd function for c 2b 2bgcd cpp codegcd 28 29 c 2b 2bget gcd in c 2b 2bgcd function in c 2b 2b stlgcd function c 2b 2b 2bfind gcd in c 2b 2bgcd in c 2b 2bgcd finding using programminginbuilt function for gcd in c 2b 2bhow to find gcd c 2b 2bgcd 28a 2bc 2cb 2bc 29gcd function using asm code c 2b 2bgcd of two numbers in cppinbuilt function to find gcd cppgcd function in c 2b 2bgcd function in chcf in cpp inbuiltgreatest common divisor algorithm c 2b 2bgcd code in c 2b 2bshortest method to find gcd c 2b 2bwhat is gcd function in c 2b 2bcpp gcdgcd with functionhow to use gcd c 2b 2bcpp gcdc 2b 2b built in gcdinbult gcd oin c 2b 2bgcd of two numbers in c 2b 2bgcd 28k 2ba 2ck 2bb 29greatest common divisor practice problems c 2b 2bgcd of two numbers in c 2b 2b stlgcd function c 2b 2b stlgcd c 2b 2b referencegcd in c programgcd in stlgcd algorithm c 2b 2bfind gcd in cpp gcd function c 2b 2b hackerearthgcd 283 2c26 29 in c 2b 2bgcd stl c 2b 2bgcd c 2b 2b stl gcd in c 2b 2bgcd 28n 2ck 29gcd of 2 numbers c 2b 2bgreatest common divisor c 2b 2bgcd in c program with functionfinding gcd cppgcd of array in cppc 2b 2b gcdgcd program in cppgcd c 2b 2b codegcd function cppprogram to find gcd of two numbers in c 2b 2bfind gcd functioncalaculating gcd c 2b 2bgcd library c 2b 2bgcd inbuilt function in c 2b 2bgcd with stl c 2b 2bgcd cppis there any gcd function in c 2b 2bgcd in c 2b 2b stlgcd template c 2b 2bgcd function c 2b 2b time complexitygcd function in cpp gcd header filegcd using cppgcd built in c 2b 2bgcd function c 2b 2bc 2b 2b find gcdgcd cp algorithmspredefined functions of lcm and gcd in cgcd code in cppinbuilt function of gcd in c 2b 2bgcd in cppgcd of string c 2b 2bgdc c 2b 2bbuilt in gcd function in c 2b 2b gcd 28 29how to find gcd of two numbers in cppc 2b 2b program to calculate gcd gcd cppgreatest common divisor in c 2b 2bhow to use built in gcd in c 2b 2binbuilt function to find gcd c 2b 2bgcd c programfind gcd c 2b 2bhcf function in c 2b 2bgcd 28a 2bb 2c a 2bc 29wap to find gcd of two numbers in c 2b 2bgcd of vector c 2b 2bgcd of an array c 2b 2bfunction to find gcd c 2b 2bfunction to find gcd in c 2b 2bgcd using cpp