Find Out GCD & LCM From Given Input by using Java Program

Code:

import java.util.*;

public class GCD_LCM
{
    public static void main(String[] args)
    {
       Scanner sc = new Scanner(System.in);

       System.out.print("\nEnter The 1st Number : ");
       int a = sc.nextInt();

       System.out.print("\nEnter The 2nd Number : ");
       int b = sc.nextInt();

       int p=0,m=a,n=b;

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

        int gcd = a;
        int lcm = (m*n)/gcd;

        System.out.println("\nGCD : "+gcd+"\n"+"\nLCM : "+lcm+"\n");
     }
}

Output of program:



No comments

Powered by Blogger.