Find the GCD using Recursion in Java

In this blog I am provide you the code for Generating GCD using Recursion in java.

import java.io.*;

class GCD

{

                int gcd(int m,int n)

                {

                                if(n==0)

                                                return m;

                                else if(n>m)

                                                return gcd(n,m);

                                else

                                                return gcd(n,m%n);

                }

                public static void main(String[] args)

                {

                                System.out.print("Enter num1 : ");

                                int num1= Integer.parseInt(System.console().readLine());

                                System.out.print("Enter num2 : ");

                                int num2= Integer.parseInt(System.console().readLine());

                                GCD obj=new GCD();

                                System.out.println();

                                System.out.println("GCD of "+num1+" and "+num2+" is "+obj.gcd(num1,num2));

                }

}


Output: -

Leave Comment