Leave a comment

Find commong GCD for two numbers using recursion.

This is a simple program. Pasting it below.


public class FindGCD {
    public static void main (String s[]) {
        System.out.println(gcd(48, 24));
    }

    public static int gcd (int x, int y) {
        if (x > y) {
            return findGcd (x, y);
        } else {
            return findGcd (y,x);
        }
    }

    private static int findGcd (int l, int s) {
        if (l % s == 0) {
            return s;
        } else {
            return findGcd (s, l % s);
        }
    }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: