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 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 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 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.

Sachindra N. Pandey said...

its true....:)

Anonymous said...

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

Unknown 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 ?

Unknown said...

plz anybody tell program to find lcm of 3 numbers

Ashish Gupta said...
This comment has been removed by the author.
Unknown 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);
}

Ashish Gupta 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

Unknown 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 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)

Unknown 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);
}
}
}
}

Unknown said...

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

Unknown 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*/

Unknown 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*/

Unknown 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...
This comment has been removed by the author.
Pramod Monangi said...
This comment has been removed by the author.