From 99f25ca627986ffef937b338457d56a367329fc8 Mon Sep 17 00:00:00 2001 From: rish9898 <39212648+rish9898@users.noreply.github.com> Date: Thu, 14 Feb 2019 04:33:42 +0530 Subject: [PATCH] Add C++ code (#25437) * Add C++ code * fix: removed recursive version --- .../greatest-common-divisor-euclidean/index.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/guide/english/algorithms/greatest-common-divisor-euclidean/index.md b/guide/english/algorithms/greatest-common-divisor-euclidean/index.md index ae268084df..147157f065 100644 --- a/guide/english/algorithms/greatest-common-divisor-euclidean/index.md +++ b/guide/english/algorithms/greatest-common-divisor-euclidean/index.md @@ -65,6 +65,19 @@ function gcd(a, b) { } ``` +C++ Code to Perform GCD- +```csharp +int gcd(int a,int b) { + int R; + while ((a % b) > 0) { + R = a % b; + a = b; + b = R; + } + return b; +} +``` + Python Code to Perform GCD using Recursion ```Python def gcd(a, b): @@ -87,7 +100,6 @@ static int gcd(int a, int b) ``` - You can also use the Euclidean Algorithm to find GCD of more than two numbers. Since, GCD is associative, the following operation is valid- `GCD(a,b,c) == GCD(GCD(a,b), c)`