Hey, I'm new here, and because I've create this account, is to learn from others based on their opinions and suggestions. Now, I'm studying computer engineering and I'm learning how to programming in C++ language as introduction. And I've this program... I'm working on "functions", in my code I've 4 CPP files and 3 headers, and I want to get the gcd of two integers.
The problem is :
First of all, I've read the euclid's theorem, and I understand it, but...
Let's see :
// A program that obtain the result gcd of two integers.
#include <iostream>
#include <iomanip>
using namespace std;
int gcd( int a, int b);
int main() {
int a, b;
cout << "Enter two integers: ";
cin >> a >> b;
cout << "The greatest common divisor of" << a << "and" << b << "is" <<
gcd(a, b) << endl;
Thanks for the help. I've already try what you're suggesting, but somehow it won't work... Maybe I was wrong, but then I decided to try once! Again! And this is what I got :
int gcd( int a, int b) {
int r;
while ( a > b || a < b ) {
r = a % b;
a = b;
b = r;
if ( r == 0 ) {
return(b);
} else
{
r = b % a;
b = a;
a = r;
return(a);
}
}
}
And wow, I'm very excited... For now :-) thanks for your time, I appreciated.