Write a c program to find out L.C.M. of two numbers.





LCM program in c with two numbers : 

#include<stdio.h>
int main(){
  int n1,n2,x,y;
  printf("\nEnter two numbers:");
  scanf("%d %d",&n1,&n2);
  x=n1,y=n2;
  while(n1!=n2){
      if(n1>n2)
           n1=n1-n2;
      else
      n2=n2-n1;
  }
  printf("L.C.M=%d",x*y/n1);
  return 0;
}

LCM program in c with two numbers (Other logic) :



#include<stdio.h>

int lcm(int,int);

int main(){

   

    int a,b,l;

    printf("Enter any two positive integers ");
    scanf("%d%d",&a,&b);
 
    if(a>b)
         l = lcm(a,b);
    else
         l = lcm(b,a);
   
    printf("LCM of two integers is %d",l);

    return 0;
}

int lcm(int a,int b){
  
    int temp = a;

    while(1){
         if(temp % b == 0 && temp % a == 0)
             break;
         temp++;
    }

   return temp;
}

LCM program in c with multiple numbers :




#include<stdio.h>

int lcm(int,int);



int main(){

   
    int a,b=1;
    printf("Enter positive integers. To quit press zero.");

    while(1){
         scanf("%d",&a);
         if(a<1)
             break;
         else if(a>b)
             b = lcm(a,b);
         else
             b = lcm(b,a);
    }

    printf("LCM is %d",b);

    return 0;
}

int lcm(int a,int b){
 
    int temp = a;

    while(1){
         if(temp % b == 0 && temp % a == 0)
             break;
         temp++;
    }

    return temp;
}





Definition of LCM (Least common multiple):

LCM of two integers is a smallest positive integer which is multiple of both integers that it is divisible by the both of the numbers.
For example: LCM of two integers 2 and 5 is 10 since 10 is the smallest positive numbers which is divisible by both 2 and 5.




24 comments:

Anonymous said...

#include
#include
voidmain()
{
inta,b,n,m,gcd,lcm;
clrscr();
printf("enter the values of a,b");
scanf("%d%d",&a,&b);
n=a;
m=b;
while(n>m)
{
if(n>m)
n=n-m;
else
m=m-n;
}
gcd=n;
lcm=a*b/gcd
printf("lcm of %d and %d is %d",a,b,lcm);
getch();
}

riya said...

for loop se eassy hoga !!!!!!!

Anonymous said...

/*Best way to calculate an LCM*/


#include
main()
{
int a,b,i,max,min;
scanf("%d %d",&a,&b);

if(b>a)
{
max = b;
min = a;
}
else
{
max = a;
min = b;
}

for(i=1;;i++)
{
if((i*max)%min==0)
{
printf("LCM : %d\n",i*max);
break;
}
}

}

Anand Barnwal said...

No,for loop doesn't make the program easy.
It only allows us to specify 3 things about a loop in single line.



Anand Barnwal said...

Code can be made even smaller. Like this:

#include
main()
{
int a, b, i =1;
scanf("%d %d", &a, &b);
for(; a != b; i++)
{
if((a*i)%b == 0)
break;
}
printf("LCM is %d", a*i);
}

Anand Barnwal said...

It is not necessary to find the max or min number.
You can directly proceed. You have to take care only when a equals to b.

A SPRITUAL GUY said...

its true....:)

Anonymous said...

ur program is not compiling with 2 and 5 or any other no. which have no common factors

Adam Villafana said...

What about with 3 numbers?

Anonymous said...

can any one tell, how to write code for LCM of 2 numbers without calculating GCD, using Transform-and-Conquer method ?

yamini ashok said...

plz anybody tell program to find lcm of 3 numbers

Aucky said...
This comment has been removed by the author.
Asad Rabbi said...

This is the most efficient program___

#include
void main()
{
int a, b, i, x, y, gcd, lcm;

scanf("%d%d",&a,&b);
x=a;
y=b;

if(a==0)
gcd=a;
if(b==0)
gcd=b;
else{
while(b!=0){
i=b;
b=a%b;
a=i;
}
}
gcd=a;

lcm=(x*y)/gcd;

printf("%d",lcm);
}

Aucky said...

// LCM of three numbers

#include
int main()
{
int num1, num2, num3, max;
printf("Enter three positive integers: ");
scanf("%d %d %d", &num1, &num2,&num3);
max=(num1>num2) ? num1 : num2;
max= (max>num3) ? max : num3;
while(1)
{
if(max%num1==0 && max%num2==0 && max%num3)
{
printf("LCM of %d, %d and %dis %d", num1, num2,num3,max);
break;
}
++max;
}
return 0;
}

For more information visit: www.techedinfo.com

Soumalya said...

its working

sandeep chaurasia said...

#include
int main ()
{
int t,i,j,a,b,h;
scanf("%d%d",&a,&b);
for(i=1,j=1 ;i<=a&&j<=b;i++,j++)
{
if(a%i==0 && b%j==0)
{
t=i;
}
}
printf("LCM- %d",t);
h=a*b/t;
printf("HCF- %d",h);
return 0;
}

rahul kumar said...

if(max%num1==0 && max%num2==0 && max%num3) typing mistake in above statement
if(max%num1==0 && max%num2==0 && max%num3==0)

Nadine Puth said...

//LCM
#include
int main(){
int a,b,i=1,s;
printf("Insert a: ");scanf("%d",&a);
printf("Insert b: ");scanf("%d",&b);
if (a==b){
printf("%d",a);
}
else if (a>b){
s=b;
while (b%a!=0){
b=s*i;
i++;
if (b%a==0){
printf("LCM of three numbers is %d",b);
}
}
}
else{
s=a;
while (a%b!=0){
a=s*i;
i++;
if (a%b==0){
printf("LCM of three number is %d",a);
}
}
}
}

Adan ashar said...

Anybody tell me plz Write a function LCM() that receives two integer arguments and returns LCM.

kamisetty tejyapranthi said...


int main()
{
int a,b,c,i,m,l;
printf("enter a b c values:");
scanf("%d%d%d",&a,&b,&c);
/* if(a>b&&a>c)
l=a;
else if(b>c)
l=b;
else
l=c;*/
((a>b)&&(a>c))?l=a:b;
(b>c)?l=b:l=c;
for(i=1;i<=10000;i++)
{
m=l*i;
if(m%a==0&&m%b==0&&m%c==0)
{
printf("lcm of numbers is:%d",m);
break;
}
}
}
/*OUTPUT:-
enter a,b,c values :17 18 19
lcm of numbers:5814*/

kamisetty tejyapranthi said...


int main()
{
int a,b,c,i,m,l;
printf("enter a b c values:");
scanf("%d%d%d",&a,&b,&c);
/* if(a>b&&a>c)
l=a;
else if(b>c)
l=b;
else
l=c;*/
((a>b)&&(a>c))?l=a:b;
(b>c)?l=b:l=c;
for(i=1;i<=10000;i++)
{
m=l*i;
if(m%a==0&&m%b==0&&m%c==0)
{
printf("lcm of numbers is:%d",m);
break;
}
}
}
/*OUTPUT:-
enter a,b,c values :17 18 19
lcm of numbers:5814*/

divya sri said...

#include
#include
void main()
{
int x,y,a;
int lcm(int,int);
printf("enter two numbers:");
scanf("%d\t%d",&x,&y);
if(x>y)
{
a=lcm(x,y);
}
else
{
a=lcm(y,x);
}
printf("lcm of two numbers:%d",a);
getch();
}
int lcm(int x,int y)
{
int n=x;
while(1)
{
if(n%x==0&&n%y==0)
break;
n++;
}
return n;
}

pramod monangi said...

#include
int main()
{
int n[50]={2,3,5,7,11,13,17,19,23,29,31,37},i,lcm=1,a,b,c;
printf("enter values\n");
scanf("%d%d%d",&a,&b,&c);
for(i=0;;i++)
{
while((a%n[i]==0)||(b%n[i]==0)||(c%n[i]==0))
{
if(a%n[i]==0) a/=n[i];
if(b%n[i]==0) b/=n[i];
if(c%n[i]==0) c/=n[i];
lcm*=n[i];
}
if(a==1&&b==1&&c==1)
break;
}
printf("LCM = %d\n",lcm);
return 0;
}

pramod monangi said...

Every code other than mine takes more than 5 seconds run time for numbers greater than 100.