Program to find lcm of two number


Prev Tutorial Next Tutorial

LCM of two number

To find the LCM (Least Common Multiple) of two or more numbers, make multiple of numbers and choose common multiple. Then take lowest common multiple, this lowest common multiple is LCM of numbers. For example;

Example

Suppose find LCM of 3 and 4
Multiple of 3 : 3, 6, 9, 12, 15, 18, 21, 24,.......
Multiple of 4 : 4, 8, 12, 16, 20, 24, 28,.....

So, common multiple of 3 and 4 is 12, 24
and Least Common Multiple is 12
LCM of 3 and 4 is: 12

LCM of two number

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

void lcm(int,int);

void main()
{
    int a,b;
    clrscr();
    printf("Enter two numbers: ");
    scanf("%d %d",&a,&b);
    lcm(a,b);
    getch();
  //  return 0;
}

//function to calculate l.c.m
void lcm(int a,int b)
{
    int m,n;

    m=a;
    n=b;

    while(m!=n)
    {
	if(m < n)
	{
	m=m+a;
	}
	else
	{
	    n=n+b;
	 }
    }

    printf("\nL.C.M of %d and %d is: %d",a,b,m);
}

Output

Enter any two number: 
3
15
L.C.M of 3 and 15 is: 15

Prev Tutorial Next Tutorial

Advertisements




Facebook Likes

Advertisements