Search
 
SCRIPT & CODE EXAMPLE
 

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;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ print text 
Cpp :: Nested if...else 
Cpp :: initialize a vector to 0 
Cpp :: how to find even and odd numbers in c++ 
Cpp :: Traversing a C++ Array 
Cpp :: how to reverse a vector in c++ 
Cpp :: c++ vector first element 
Cpp :: variables in c++ 
Cpp :: standard template library in c++ 
Cpp :: how to sort array in c++ stockoverflow 
Cpp :: c++ check if key exists in map 
Cpp :: initialising 2d vector 
Cpp :: c++ namespace example 
Cpp :: c++ sizeof 
Cpp :: Pseudocode of Dijkstra’s Algorithm in C++ 
Cpp :: how to traverse through vector pair 
Cpp :: glfw error: the glfw library is not initialized 
Cpp :: how to concatinate two strings in c++ 
Cpp :: prime number program c++ 
Cpp :: c++ linked list delete node 
Cpp :: greatest and smallest in 3 numbers cpp 
Cpp :: string append at position c++ 
Cpp :: cpp vector structure 
Cpp :: std::string substr 
Cpp :: build a prefix array cpp 
Cpp :: how to set arrays as function parameters in c++ 
Cpp :: An Array declaration by initializing elements in C++ 
Cpp :: vsearch c program stdlib 
Cpp :: c++ throw index out of bound 
Cpp :: 1822. Sign of the Product of an Array leetcode in c++ 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =