Purchase Solution

Greatest Common Divisor (GCD) in C++ with Example

Not what you're looking for?

Ask Custom Question

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 <stdio.h>
#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;
}

Attachments
Purchase this Solution

Solution Summary

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

Solution Preview

The following is a c++ version of the above program to calculate Greatest Common Divisor of two integers.

#include <iostream>
...

Purchase this Solution


Free BrainMass Quizzes
Java loops

This quiz checks your knowledge of for and while loops in Java. For and while loops are essential building blocks for all Java programs. Having a solid understanding of these constructs is critical for success in programming Java.

Inserting and deleting in a linked list

This quiz tests your understanding of how to insert and delete elements in a linked list. Understanding of the use of linked lists, and the related performance aspects, is an important fundamental skill of computer science data structures.

Basic Networking Questions

This quiz consists of some basic networking questions.

Word 2010: Table of Contents

Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.

Javscript Basics

Quiz on basics of javascript programming language.