You are on page 1of 1

//==================================================================

//Exercise 5 : Write a program to find the GCD and LCM of two no�s.
//==================================================================
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
//------------------------------------------------
// Function to compute GCD of numbers n and d
//------------------------------------------------
int gcd(int n,int d){
int r;
r = n%d;
while(r!=0){
n=d;
d=r;
r=n%r;
}
return d;
}
//------------------------------------------------
// Function to compute LCM of numbers n and d
//------------------------------------------------
int lcm(int n, int d){
return n * d / gcd(n,d);
}
//------------------------------------------------
// Function to to swap two numbers using reference
//------------------------------------------------
void swap(int &x,int &y){
int t;
t = x;
x = y;
y = t;
}
//------------------------------------------------
// Main Function to compute GCD and LCM of n and d
//------------------------------------------------
void main(){
int n,d;
clrscr();
cout<<"\n=======================================================";
cout<<"\nProgram to compute GCD and LCM of two integers";
cout<<"\n=======================================================\n";
cout<<"Enter the first Integer : ";cin>>n;
cout<<"Enter the second Integer : ";cin>>d;
if (n < d)
swap(n,d);
cout<<"\n=======================================================";
cout<<"\nGCD of "<<n<<" and "<<d<<" is : "<<gcd(n,d);
cout<<"\nLCM of "<<n<<" and "<<d<<" is : "<<lcm(n,d);
cout<<"\n=======================================================";
getch();
}

You might also like