We need to write a program that computes the GCD of numbers using Euclid's algorithm. Heres what I have so far, but its not working so please help. Anything is appreciated!
/* This program computes the GCD problem using Eulcid's algorithm.*/
What do you mean by "not working"? Doesn't compile? Does run? Traps? Doesn't give the right answer?
Since we don't have the libraries you're using (genlib and simpio), we can't compile or test your code.
Line 7: must be int main()
Line 17: Remove the ; That terminates the while statement.
Line 19: Statement has no effect. You're using the equality operator (==), not the assignment operator (=). Should be:
19 20
if (r==1)
n = gcd;
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
/* This program computes the GCD problem using Eulcid's algorithm.*/
#include <stdio.h>
#include "genlib.h"
#include "simpio.h"
int main()
{
int gcd,m,n,r;
printf ("What is the first number?");
m=GetInteger();
printf ("What is the second number?");
n=GetInteger();
r = m % n;
while (r>0)
{
if (r==1)
n = gcd;
elsewhile (r!=1)
{
m=n;
n=r;
gcd=r;
}
printf("The GCD of %d and %d is %d\n", m, n, gcd);
}
getchar ();
}
/* This program computes the GCD problem using Eulcid's algorithm.*/
#include <stdio.h>
#include "genlib.h"
#include "simpio.h"
main()
{
int gcd, m, n, num1, num2;
printf (" Enter first number\n");
m=GetInteger();
printf (" Enter second number\n");
n=GetInteger();
num1=m;
num2=n;
{
int r;
if((m == 0) || (n == 0))
printf ("0");
if((m < 0) || (n < 0))
printf ("1");
do
{
r = m % n;
if(r == 0)
break;
else
m = n;
n = r;
gcd = m%n;
}
printf("The GCD of %d and %d is %d\n", m, n, gcd);
getchar();
}