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.




11 comments:

  1. #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();
    }

    ReplyDelete
  2. for loop se eassy hoga !!!!!!!

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



      Delete
  3. /*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;
    }
    }

    }

    ReplyDelete
    Replies
    1. 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);
      }

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

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

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

    ReplyDelete
  6. plz anybody tell program to find lcm of 3 numbers

    ReplyDelete