From 417e6fc667ceb8fe869ab84c2271448eff661a6a Mon Sep 17 00:00:00 2001 From: Pratibha Verma <141310751+Pratibha65@users.noreply.github.com> Date: Mon, 30 Oct 2023 23:12:10 +0530 Subject: [PATCH] Create GCD.cpp This is a recursive code is to find GCD of two numbers. --- C++ Code/Number Theory/GCD.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 C++ Code/Number Theory/GCD.cpp diff --git a/C++ Code/Number Theory/GCD.cpp b/C++ Code/Number Theory/GCD.cpp new file mode 100644 index 0000000..fd5d85b --- /dev/null +++ b/C++ Code/Number Theory/GCD.cpp @@ -0,0 +1,31 @@ +// C++ program to find GCD of two numbers + +#include +using namespace std; + +int gcd(int a, int b) +{ + + if (a == 0) + return b; + if (b == 0) + return a; + + + if (a == b) + return a; + + + if (a > b) + return gcd(a - b, b); + return gcd(a, b - a); +} + + +int main() +{ + int a = 98, b = 56; + cout << "GCD of " << a << " and " << b << " is " + << gcd(a, b); + return 0; +}