Answers for "how to find gcd of two numbers in c++"

C++
3

gcd of two numbers c++

// gcd function definition below:
int gcd(int a, int b) {
   if (b == 0)
   return a;
   return gcd(b, a % b);
}

int a = 105, b = 30;
cout<<"GCD of "<< a <<" and "<< b <<" is "<< gcd(a, b);
// output = "GCD of 105 and 30 is 15";
Posted by: Guest on September-16-2020
2

gcd 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);
}
Posted by: Guest on September-19-2020
0

how to find gcd of two numbers in c++

#include<bits/stdc++.h>
using namespace std;
long long UCLN(long long a,long long b)
{
    long long r;
    while (b!=0)
    {
        r=a%b;
        a=b;
        b=r;
    }
    return a;
}


int main()
{
    long long a, b;
    cout<< "enter: ";
    cin>> a;
    cout<< "enter: ";
    cin >>b;
    cout<<UCLN(a,b)<<endl;
    return 0;
}
Posted by: Guest on October-03-2021

Code answers related to "how to find gcd of two numbers in c++"

Browse Popular Code Answers by Language