Thursday, 23 April 2015

LCM

import java.io.InputStreamReader;
import java.io.BufferedReader;

public class hLCM {
    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 LCM : ");
        try {
            first = Integer.parseInt(reader.readLine());
            second = Integer.parseInt(reader.readLine());
        } catch (Exception e) {
            e.printStackTrace();
           
        }

        int lcm = first * ( second / gcd(first , second ) );
               
       
        System.out.println("LCM is "  + lcm );

    }

    private static int gcd(int first, int second) {
        while (second > 0) {
            int temp = second;
            second = first % second;
             first = temp ;

        }
        return first ;
    }
}

No comments:

Post a Comment