Answers for "gcd using cpp"

C++
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
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