Find gcd, lcm of given two numbers.


 Hello, budding developers.

As the title says it all. We will write a program to find the gcd and lcm of two given numbers.

Editor used: CodeBlocks

Language used: C++

Program:

#include <iostream>

using namespace std;

int main()

{

    int n1,n2,gcd=0,lcm=0;

   cout<<"Enter number 1:";

   cin>>n1;

   cout<<"\nEnter number 2:";

   cin>>n2;

 

   int mult=n1*n2;

   while(n1!=n2)

   {

       if(n1>n2)

       {

           n1=n1-n2;

           gcd=n1;

       }

       else

       {

           n2=n2-n1;

           gcd=n2;

       }

   }

   cout<<"GCD is: "<<gcd<<"\n";

   lcm=(mult)/gcd;

   cout<<"LCM is: "<<lcm;

    return 0;

}


Output:





Comments

Popular posts from this blog

Expense Manager or Expense Tracker app

Return smallest and largest number by rearranging digits.