by Dinesh Thakur Category: Java Methods

 

import java.io.*;

class CommonDivisor
{
            int gcd(int m, int n)
            {
                        int r;
                        if (m < n) return gcd(n,m);
                        r = m%n;
                        if (r == 0) return(n);
                        else return(gcd(n,r));
            }
                        public static void main(String args[]) throws IOException
            {
                        CommonDivisor Divisor = new CommonDivisor();
                 BufferedReader BuffRead = new BufferedReader(new InputStreamReader(System.in));
                        String Num;
                        int a,b,Div;
                        System.out.print("Enter a Number :");
                        Num=BuffRead.readLine();
                        a=Integer.parseInt(Num);
                        System.out.print("Enter Another Number :");
                        Num=BuffRead.readLine();
                        b=Integer.parseInt(Num);
                        Div=Divisor.gcd(a,b);
                        System.out.print("The Greatest Common Divisor is "+Div);
            }
}

Greatest Common Divisor Using Recursion



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.