C++
#include<iostream>
using namespace std;
void takeinput(int &n1,int &n2)
{
cout << “Enter 1st number: “;
while(!(cin>>n1))
{
cin.clear();
while(cin.get()!=’\n’)
continue;
cout << “Enter 1st number: “;
}
cout << “Enter 2nd number: “;
while(!(cin>>n2))
{
cin.clear();
while(cin.get()!=’\n’)
continue;
cout << “Enter 2nd number: “;
}
}
int calgcd(int n1,int n2)
{
int gcd,i;
for (i=1; i <=n2;i++)
{
if (n1 % i == 0 && n2 % i ==0)
{
gcd = i;
}
}
return gcd;
}
int main()
{
int n1, n2, gcd;
takeinput(n1,n2);
// Swapping variables n1 and n2 if n2 is greater than n1.
if ( n2 > n1)
{
n1=n1+n2;
n2=n1-n2;
n1=n1-n2;
}
gcd=calgcd(n1,n2);
cout << “GCD = ” << gcd;
return 0;
}