C++ program to find gcd of two numbers. - coderforcoders

Search

Wednesday, 13 March 2019

C++ program to find gcd of two numbers.

#include<iostream>
using namespace std;
class gcd{
public:
int num1,num2,gcd;
void findGcd()
{
if(num1==0 || num2==0)
{
cout<<"You can't enter zero\n";
return;
}
while(num1!=num2)
{
if(num1>num2)
num1-=num2;
else
num2-=num1;
}
cout<<"Gcd is "<<num1;
}
};
int main()
{
gcd obj1;
cout<<"Please enter the first number\n";
cin>>obj1.num1;
cout<<"Please enter the second number\n";
cin>>obj1.num2;
obj1.findGcd();
cout<<endl;
}


Output 1:
Please enter the first number
6
Please enter the second number
9
Gcd is 3

Output 2:
Please enter the first number
89
Please enter the second number
76
Gcd is 1

Output 2:
Please enter the first number
45
Please enter the second number
15
Gcd is 15



No comments:

Post a Comment