Find LCM of two numbers in C++


Prev Tutorial Next Tutorial

C++ program to find LCM of two numbers

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;

LCM

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

C++ program to find LCM of two numbers


#include<iostream.h>
#include<conio.h>

void lcm(int,int);

void main()
{
    int a,b;
    clrscr();
    cout<<"Enter two numbers: ";
    cin>>a;
    cin>>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;
	    }
    }

    cout<<"\nL.C.M of "<<a<<" and "<<b<<" is "<<m;
}

Example

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

Prev Tutorial Next Tutorial

Google Advertisements
Download Projects

Google Adsense Advertisements

Buy 3 Year Old Domain, Tutorialday.com, SeosmoTools.com @$200

Buy This Ad Space @$50 per Month, Ad Size 300X250 Contact on: hitesh.xc@gmail.com WhatsApp no. 8882538826

Yahoo Advertisements