Tuesday, March 18, 2014

Euclid's Algorithm to calculate the GCD and LCM of two given positive integers

1:  package com.mridul.java.dump;  
2:  import java.io.BufferedReader;  
3:  import java.io.IOException;  
4:  import java.io.InputStreamReader;  
5:  /**Program to compute GCD and LCM of two numbers  
6:   * by using EuclidsAlgorithm  
7:   * @author Mridul Jayan  
8:   * @date 03/18/2014  
9:   */  
10:  public class EuclidsAlgorithm {  
11:       /*  
12:        * main method  
13:        */  
14:       public static void main(String[] args) {  
15:            try{  
16:                 int gcd=0,lcm=0;  
17:                 EuclidsAlgorithm ea=new EuclidsAlgorithm();  
18:                 int num1=Integer.parseInt(ea.readConsoleInput(  
19:                           "Enter the first number : "));  
20:                 int num2=Integer.parseInt(ea.readConsoleInput(  
21:                           "Enter the second number : "));  
22:                 //re-align parameters based on size  
23:                 if(num1<0 || num2<0){  
24:                      System.out.println("\nPlease do not enter negative integers.");  
25:                 }else if(num1==0 || num2==0){  
26:                      System.out.println("\nThe GCD and LCM if one input is 0 is 0.");  
27:                 }else{  
28:                      if(num1<num2){  
29:                           int temp=num1;  
30:                           num1=num2;  
31:                           num2=temp;  
32:                      }  
33:                      //calculate gcd and lcm  
34:                      gcd=ea.computeGCD(num1,num2);  
35:                      lcm=ea.computeLCM(num1,num2,gcd);  
36:                      System.out.println(String.format(  
37:                                "\nThe GCD is %d and the LCM is %d\n"  
38:                                +"for the numbers %d and %d."  
39:                                ,gcd,lcm,num1,num2));  
40:                 }  
41:            }catch(Throwable t){  
42:                 t.printStackTrace();  
43:            }  
44:       }  
45:       /*  
46:        * method to read input from console   
47:        */  
48:       private String readConsoleInput(String message) throws IOException{  
49:            System.out.println(message);  
50:            //read input from console  
51:            BufferedReader br=new BufferedReader(  
52:                      new InputStreamReader(System.in));       
53:            return br.readLine();       
54:       }  
55:       /*  
56:        * method to compute GCD  
57:        * using Euclid's Algorithm  
58:        */  
59:       private int computeGCD(int large, int small){  
60:            int rem;  
61:            do{  
62:                 rem=large%small;  
63:                 large=small;  
64:                 small=rem;  
65:            }while(rem>0);  
66:            //large will contain the gcd  
67:            System.out.println("large : "+large);  
68:            return large;  
69:       }  
70:       /*  
71:        * method to compute LCM  
72:        * as a result of the product of the numbers  
73:        * divided by their GCD  
74:        */  
75:       private int computeLCM(int large, int small, int gcd){  
76:            return ((large*small)/gcd);  
77:       }  
78:  //End of EuclidsAlgorithm       
79:  }  

No comments:

Post a Comment