Answers for "lcm stl c++"

C++
1

lcm in c++

#include <iostream>  
using namespace std;  
int main()  
{  
    int n1, n2, max_num, flag = 1;  
    cout << " Enter two numbers: \n";  
    cin >> n1 >> n2;      
      
    // use ternary operator to get the large number  
    max_num = (n1 > n2) ? n1 : n2;  
      
    while (flag)    
    {  
        // if statement checks max_num is completely divisible by n1 and n2.  
        if(max_num % n1 == 0 && max_num % n2 == 0)  
        {  
            cout << " The LCM of " <<n1 << " and " << n2 << " is " << max_num;  
            break;  
        }  
        ++max_num; // update by 1 on each iteration  
    }  
    return 0;  
}
Posted by: Guest on October-27-2021
0

LCM and HCF using c++

#include<iostream>
using namespace std;
int gcd(int a,int b)
{
    if(a%b==0)
    return b;
    else
    gcd(b,a%b);
}
int main()
{
int a,b;
cin>>a>>b;
int res = gcd(a,b);
cout<<"HCF : "<<res<<endl;
cout<<"LCM :"<<(a*b)/res;
return 0;
}
Posted by: Guest on October-20-2021
0

lcm function c++

ll lcm(ll a,ll b)
{
    return (a*b)/gcd(a,b);
}
Posted by: Guest on September-19-2021

Browse Popular Code Answers by Language