C++ program to find GCD and LCM of two numbers

/*GCD of two numbers is the greatest common divisor that
divides both the numbers completely e.g GCD of 15 and 25 is 5.
LCM of two numbers is the product of two numbers divide by its GCD
e.g LCM of 15 and 25 is 15x25/5(gcd)=75 */

#include <iostream>
using namespace std;

int main()
{
   int a,b,gcd,lcm;
   cout<<"Enter two numbers to find its GCD and LCM \n";
   cout<<"Enter first Number ";
   cin>>a;
   cout<<"Enter second Number ";
   cin>>b;

   for(int i=1;i<=a;i++)
   {
      //condition to find highest divisor of both numbers
      if((a%i==0)&&(b%i==0))
      {
         gcd=i;
      }

   }

     cout<<"\nThe GCD Of two numbers "<<a<<" and "<<b<<" is "<<gcd<<endl;
      //formula to find LCM
      lcm=(a*b)/gcd;
     cout<<"The LCM Of two numbers "<<a<<" and "<<b<<" is "<<lcm<<endl;

    return 0;
}


No comments:

Post a Comment