EUCLIDEAN ALGORITHM-GCD of two numbers

Here is the Euclidean algorithm for calculating G.C.D of two numbers..

Iterative and Recursive solution:
       


//to find gcd of two numbers

//Euclidean algorithm

///RECURSIVE and ITERATIVE


#include<iostream>

using namespace std;

int gcd_iterative(int a,int b){

    while(b!=0){

        int c=a;

        a=b;

        b=c%b;

    }

    return a;

}

int gcd_recursive(int a,int b){

    if(b==0){

        return a;

    }

    gcd_recursive(b,a%b);

}

int main(){

    int a,b;

    cout<<"Enter the two numbers to fing GCD";

    cin>>a>>b;

    cout<<"GCD OF"<<a<<" AND "<<b<<" is:\n";

    cout<<gcd_recursive(a,b)<<"\n";

    cout<<gcd_iterative(a,b);

    return 0;

}


Comments