Program to Find GCD and LCM in C++.

//Program to find the GCD (Greatest Common Divisor) and LCM (Lease Common Multiplier)

#include<iostream>
#include<stdlib.h>
using namespace std;

int main()
{
long n1, n2, gcd, lcm, i, product;
system("cls");
cout<<"Enter First Number to find HCF & LCM : ";
cin>>n1;
cout<<"Enter Second Number to find HCF & LCM: ";
cin>>n2;
product=n1*n2;
for(i=1;i<=product;i++)
{
if(n1%i==0 && n2%i==0)
gcd=i;
}
lcm=product/gcd;
cout<<"GCD of "<<n1<<" and "<<n2<<" is "<<gcd<<endl;
cout<<"LCM of "<<n1<<" and "<<n2<<" is "<<lcm<<endl;
}


No comments

Post your comments

Powered by Blogger.