GCD & LCD in JAVA programming


package SolveProblem;

import java.util.Scanner;

public class GCDandLCD {

    public static void main(String[] args) {
        Scanner myScanner=new Scanner(System.in);
        int a,b;
        System.out.println("Enter two numbers : ");
        a=myScanner.nextInt();
        b=myScanner.nextInt();
       
        int small;
       
        if(a<b)
            small=a;
        else
            small=b;
        int mull=a*b;
        while(small!=0){
            if ((a%small==0)&&(b%small==0)) {
                System.out.println("GCD is :"+small+" & LCD is : "+(mull/small));
                break;
            }else{
                small--;
            }
        }
       
       
    }

}
//Enter two numbers :
//10 2
//GCD is :2 & LCD is : 10

No comments:

Post a Comment