GCD program

closed account (DzyTC542)
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.*/

#include <stdio.h>
#include "genlib.h"
#include "simpio.h"

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;
while (r!=1)
{
r=m%n;
m=n;
n=r;
gcd=r;
}
printf("The GCD of %d and %d is %d\n", m, n, gcd);
}
getchar ();
}


Thank you!:)
Last edited on
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/
Last edited on
closed account (DzyTC542)
Hi, sorry about that. I meant that the output is blank, even after doing what you said. My code looks like this now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/* 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;
    else while (r!=1)
    	{
    		m=n;
    		n=r;
    		gcd=r;
		}
	printf("The GCD of %d and %d is %d\n", m, n, gcd);
	}
getchar ();
} 

Maybe my compiler is not working?
A couple problems I see:

Line 20: gcd is used without being initialized.

Line 27: You probably want the printf outside the loop. i.e. if r is 0 at line 14, the printf will never execute.

closed account (DzyTC542)
Ok. Thanks. I changed my program and here's what I have now. Rather than getting the GCD, my output is just a _ . Do you know what's the problem?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* 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();
}
Topic archived. No new replies allowed.