Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

c++ program to find lcm of two numbers

#include<iostream>
using namespace std;

int main(){
  int num1,num2,temp,lcm,hcf;
  cout<<"Enter 1st number"<<endl;
  cin>>num1;
  cout<<"Enter 2nd number"<<endl;
  cin>>num2;
  if (num1>=num2){
    temp=num1;
    num1=num2;
    num2=temp;
  }
  for (int i=1 ; i<=num1 ; i++){
    if (num1%i==0 && num2%i==0){
      hcf=i;
    }
  }
  // MATHEMATICAL FORMULA : HCF * LCM = NUM1 * NUM2
  lcm = (num1*num2)/hcf;
  cout<<"Least Common Multiple of "<<num1<<" and "<<num2<<" is "<<lcm<<endl;
  return 0;
}
 
PREVIOUS NEXT
Tagged: #program #find #lcm #numbers
ADD COMMENT
Topic
Name
4+8 =