Wednesday 9 October 2013

Program to check GCD and LCM of two nos using cmdline input

//Program to check GCD and LCM of two nos using cmdline input
//Developed by Ahlam Ansari

import java.io.*;
class gcdLcm
{
    public static void main(String ahlam[])
    {
          int a, b, x, y, t, gcd, lcm;
        
          x=Integer.parseInt(ahlam[0]);
        y=Integer.parseInt(ahlam[1]);

         a = x;
        b = y;

         while (b != 0)
        {
            t = b;
            b = a % b;
            a = t;
        }

         gcd = a;
          lcm = (x*y)/gcd;

          System.out.print("Greatest common divisor of "+ x + "and " + y + "= " + gcd);
          System.out.print("Least Common Multiple of "+ x + "and " + y + "= " + lcm);
    }
}


/*

C:\Users\Ahlam\Google Drive\OOPM\Programs>java GCDLCM 2 3
Enter two integers
Greatest common divisor of 2and 3= 1Least Common Multiple of 2and 3= 6
C:\Users\Ahlam\Google Drive\OOPM\Programs>

*/

No comments:

Post a Comment