Sunday, May 4, 2014

Calculate LCM and GCF

Ques: 

Calculate LCM and GCF  of 2 numbers

Ans: 


The formula used for LCM = a*b/gcf(a,b)
for GCF we have used Euclid's approach.

package createlcm;
public class CreateLcm {

    public static void main(String[] args) {

        Integer result=0;
        result=gcfcalc(22,13);
        System.out.println("the gcf is=="+result);
        System.out.println("the lcm for numbers is== "+LCMcalc(16,10));
    }
    public static Integer LCMcalc(int a,int b){
         return (a*b)/gcfcalc(a,b);
           }
    public static Integer gcfcalc(int c, int d){
        int inum=0;
        while(c%d!=0){
                 inum=c;
            c=d;
            d=(inum%d);
                  }
        return(d);
    }
}

No comments:

Post a Comment