From dec07c31855d06d45aa778ebf5644ffb18c420bc Mon Sep 17 00:00:00 2001 From: sanu-coder <72346984+sanu-coder@users.noreply.github.com> Date: Tue, 13 Oct 2020 22:31:54 +0530 Subject: [PATCH] Create GCD of two numbers --- GCD of two numbers | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 GCD of two numbers diff --git a/GCD of two numbers b/GCD of two numbers new file mode 100644 index 00000000..416a575f --- /dev/null +++ b/GCD of two numbers @@ -0,0 +1,28 @@ +class Test +{ + // Recursive function to return gcd of a and b + static 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; + + + if (a > b) + return gcd(a-b, b); + return gcd(a, b-a); + } + + // Driver method + public static void main(String[] args) + { + int a = 98, b = 56; + System.out.println("GCD of " + a +" and " + b + " is " + gcd(a, b)); + } +}