Advertisements
Advertisements
प्रश्न
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.
कोड लेखन
Advertisements
उत्तर
#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
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
