Monday, December 3, 2012

Find GCD and LCM of two Given Numbers in c++

This program will give you the gcd and lcm of two given numbers using C++ code. The logic used for this particular program is very simple and easy to understand. You need to take 5 integers values and GCD and LCM along with it. 
#include<iostream>
using namespace std;
int  main()
{
 int x,y,a,b,t,gcd,lcm;

 cout<<"Enter value of a and b:\n";
 cin>>a>>b;
 x=a;
 y=b;
 while(b!=0)
 {
  t=b;
  b=a%b;
  a=t;
  gcd=a;
  lcm=(x*y)/gcd;
 }
cout<<"\ngcd is:"<<gcd<<"\nlcm is:"<<lcm;
system("pause");
return 0;
}

No comments:

Post a Comment