Computer Science Homework Solutions
Problem
#138039

Greatest Common Divisor (GCD) in C++

I need the following problem in C++ with a recursive function and a driver programe to test the function ?


* A recursive program to calculate the Greatest
Common Divisor of two integers using the Euclidean Method.
The algorithm in non-recursive form is as follows:
EuclidGCD(a,b) {
  while (b not 0) {
swap(a,b)
b = b mod a;
  }
return a;
}
example:
gcd(356,96) = gcd(96,68) = gcd(68,28) = gcd(28,12) = gcd(12,4) = gcd(4,0) = 4
*/

#include
#define DEBUG 1

int gcd(int a, int b){
if (b==0) return a;
#ifdef DEBUG
printf("gcd(%d, %d)= ", b,a%b);
#endif
gcd(b, a%b);

}

int main(){
int a=356, b=96;

printf(" Please Enter two integers for their GCD :");
scanf("%d%d",&a,&b);
printf("%d",gcd(a,b));
return 0;
}

Attached file(s):
Attachments
c.C  View File

Solution Summary

Solution of the problem is given in C++ with a recursive function and a driver program to test the function

Solution
What is this?
By OTA - Overall OTA Rating
Purchase Cost Now
$2.19 CAD (was ~$3.99)
Included in Download
  • Plain text response
  • Attached file(s):
    • 138039-GCD.CPP.cpp
$2.19 Instant Download
Add to Cart
Why you can trust BrainMass.com
  • Your Information is Secure
  • Best Online Academic Help Service
  • Students find real academic Success
Related Solutions
Browse