Hi I'm tryingt to write this program:

Write an interactive computer program that will find the greatest common divisor of two integers using Euclid's Algorithm. The program should be independent of the order in which the two numbers are input.

EUCLID'S ALGORITHM

Divide the smaller number into the larger. If the remainder is not zero, replace the original two numbers by the remainder and the smaller of the two numbers, and repeat the division. Eventually the remainder will be zero, in which case the smaller number is the greatest common divisor.

33 121

Euclid’s Algorithm

Start
Allocate storage for variables (A, B, Remainder)
Output “Enter two values”
Input A, B
IF ((A != 0) and (B != 0))// make sure you are not dividing anything by 0
Remainder = A % B
while (Remainder != 0)
A = B
B = Remainder
Remainder = A % B
end while loop
Output “The greatest common divisior is”
Output B

end If
Stop

33 121
121 33
Use these pairs and show me how many times the loop is executed for each pair

but i cannot for the life of me get it to work right, can someone help me to see what im doing wrong? Thanks :)

``````#include <iostream>
using namespace std;
#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;
}``````
2
Contributors
1
2
Views
7 Years
Discussion Span
Last Post by Unimportant
``````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); // where did you calculate which number was smaller?
// is a%b really what you wanted here?
}``````
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.