हिंदी

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 - Computer Science 1

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
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
2023-2024 (July) Official Board Paper
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×