C program to find LCM of two numbers

C program to find LCM of two numbers

Question

Given two numbers write a program to find the LCM (Least Common Multiple) for that numbers. Example is given below…

 

As you can see in the above picture, first we find multiples of two number. ‘Least Common’ multiple implies that we have to find Least multiple which is common in both the list.

Logic

  • Check the two numbers a and b. If a is lesser then b, then increment the multiple of a. In our example a= 2 is lesser than b= 3. After increment a=4 and b=3.
  • If b is lesser than a, then increment the multiple of b. Ex: b =3 is lesser than a= 4 so after increment b =6.
  • Repeat the same process until you find a match (a==b)
  • Print the matching character.

Program

#include<stdio.h>
int main()
{
    int a,b,x,y,i=1,j=1;
    printf("enter two numbers");
    scanf("%d%d",&x,&y);
    a=x;                
    b=y;

    while(1)
    {
       if( a>b)
        {   /* Example if b = 2,j=2 then 
               b=b*j = 2*2 = 4*/
                
            b=y*j;      //increment multiple of b
            j++;
            continue;
        }
        if(b>a)
        {
            a=x*i;      //increment multiple of a
            i++;        
            continue;
        }
        if(b==a)
          {
             printf("%d",a);    //match found
             break;
          }

       }
}

 

 

 

Follow For Instant Updates

Join WhatsApp Group: link
Join our Telegram Channel: link
Like our Facebook Page:  link
Subscribe to our Youtube channel: link

Sree Hari Sanjeev

The founder of Wisdom Overflow. Software Developer at Zoho Corporation.
0 0 votes
Article Rating
Subscribe
Notify of
guest
5.4K Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
5.4K
0
Would love your thoughts, please comment.x
()
x