c program for lcm



c program for lcm. LCM stands for least common multiple. LCM of two or more numbers is the least number that is exactly divisible by each one of the given numbers. You can also refer for c porgram for hcf or gcd.

Here is an example for how we can find lcm of two number, for c program for lcm  :
lcm of 16 and 24 is 48.

c program for lcm

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

int lcm(int , int);

int main()
{
    int x,y,l;  
    printf("Enter two numbers to find lcm \n");
    scanf("%d%d",&x,&y);
    l=lcm(x,y);
    printf("\nlcm of %d & %d is %d ",x,y,l);
    getch();
}

int lcm(int a, int b)
{
    int p;

    for(int i=1;i<=a;i++)
    {
        for(int j=1;j<=b;j++)
        {
            if(b*i==a*j)
            {
                 p=b*i;
                 i=i+a;
            }
        }
    }
    return p;
}
Output of c program for lcm:
c program for lcm
c program for lcm

Alternatively, we can find lcm of two numbers with the help of gcd of that two numbers. Before that you can find c program to find gcd of two numbers. Relation between gcd and lcm is as follows.:
Let, if our number is ‘a’ and ‘b’, then lcm is,

lcm = (a*b)/gcd;

Hence the alternate C program for lcm of two number with help of gcd:

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

int gcd(int a,int h)
{
     int temp;
     while(1)
     {
          temp = a%h;
          if(temp==0)
          return h;
          a = h;
          h = temp;
     }
}

int main()
{
     int c,d,lcm,e;
     printf("enter two values to find lcm\n");
     scanf("%d%d",&c,&d);
     e=gcd(c,d);
     lcm=(c*d)/e;
     printf("\nlcm of %d and %d is %d.",c,d,lcm);
     getch();
}

Output of c program for lcm:

c program for lcm
c program for lcm



"Please Do Like Facebook Page and follow us on Twitter so that you can actively participate and develop skills in programming. If you find above post interesting do share the webpage."
This entry was posted in c program and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>