Thursday, 23 April 2015

GCD

import java.io.InputStreamReader;

import java.io.BufferedReader;

public class gGCD {
    public static void main(String args[]) {
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(in);

        int first = 0;
        int second = 0;
        // get a number
        System.out.println("Enter  numbers to find GCD : ");
        try {
            first = Integer.parseInt(reader.readLine());
            second = Integer.parseInt(reader.readLine());
        } catch (Exception e) {
            e.printStackTrace();
           
        }

        first = gcd(first , second);
       
        System.out.println("GCD is "  + first);

    }

    private static int gcd(int first, int second) {
        if(second > 0)
            first =  gcd(second , first % second) ;
        return
                first ;
   
       
    }
}

No comments:

Post a Comment