Find LCM of two numbers in C++
Advertisements
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
Google Advertisment