mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-07 20:46:16 +08:00
Added factorisation technique (#604)
* Added factorisation technique * Update and rename Math/hcf.txt to math/greatest_common_divisor.cpp * Update greatest_common_divisor.cpp
This commit is contained in:
committed by
Christian Clauss
parent
0ce3226f00
commit
525cafea94
27
math/greatest_common_divisor.cpp
Normal file
27
math/greatest_common_divisor.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
// C++ program to find GCD of two numbers
|
||||
#include <iostream>
|
||||
|
||||
// Recursive function to return gcd of a and b
|
||||
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);
|
||||
}
|
||||
|
||||
// Driver program to test above function
|
||||
int main() {
|
||||
int a = 98, b = 56;
|
||||
std::cout << "GCD of " << a << " and " << b << " is " << gcd(a, b);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user