Advertisements
Advertisements
Question
Write a C++ program to find the greatest common divisor of two numbers. Define a member function find() to accept the values and calculate GCD of two numbers and define print() function to print the GCD value.
Code Writing
Advertisements
Solution
#include<iostream.h>
#include<conio.h>
class GCD
{
int a,b, i,gcd,val;
public:
GCD()
{
gcd=1;
}
void find()
{
cout<<"Enter 2 numbers"<<endl;
cin>>a>>b;
if(a>b)
{
val=a;
}
else
{
val=b;
}
for(i=1;i<=val;i++)
{
if(a%i==0 && b%i==0)
{
gcd=i;
}
}
}
void print()
{
cout<<"GCD="<<gcd;
}
};
int main()
{
GCD g;
g.find();
g.print();
getch();
return 0;
}shaalaa.com
Is there an error in this question or solution?
