Tuesday, 18 March 2014

/* C code to find LCM of 2 numbers ,, by method 1 */

#include<stdio.h>
#include<conio.h>

main(){
    int a,b;
printf("Enter no. you want lcm");
scanf("%d%d",&a,&b);
printf("\n LCM of a and b is = %d",lcm(a,b));
return 0;
}

int lcm(int a,int b){
        int tempa,tempb;
        a=abs(a); // to ignore negative sign ,,
        b=abs(b);

        tempa=a;
        tempb=b;


        // if one of input zero(0) then lcm is always 0

        if(a==0 || b==0)
        return 0;

        while(1){      // here is main logic,,  read and get it,,
            if(tempa==tempb) return tempa;

            if(tempa<tempb)
                tempa+=a;
                else
                tempb+=b;

        }
}

No comments:

Post a Comment