Pages - Menu

Sunday, 25 July 2021

Euclidean Algorithm for GCD

  HERE IS THE CODE

#include<bits/stdc++.h>
using namespace std;
int main() {
    int a, b;
    cin>>a;
    cin>>b;
    while(a != 0 && b != 0) {
        if (a>b) {
            a  = a % b;
        } else {
            b = b % a;
        }
    }
    if (a==0) {
        cout<<b;
    }
    if (b==0) {
        cout<<a;
    }
    return 0;
}

No comments:

Post a Comment