My code... it's dead!

I am trying to write a program based on the Euclidean Algorithm that outputs the numbers that divide into the first number, but not the second, and the GCD of both numbers. Yes, it is a homework problem, but the code nearly works. It outputs everything I told it to, but not everything I want it to. Here is the code, as well as a sample output:

#include <iostream>
#include <cmath>

using namespace std;
int gcd(int a, int b);

int main()
{
static int a;
static int b;
cout << "Enter two positive integers: ";
cin >> a >> b;
cout << endl;
cout << "Divisors of " << a << " that do not divide " << b << " are: " << endl << endl;
while(b > 0)
{
if(a > b)
{
a = a-b;
cout << " " << a << endl;
}
else
{
b = b-a;
cout << " " << b << endl;
}
cout << endl;
cout << "The gcd of " << a << " and " << b << " is " << gcd(a, b) << endl << endl;
}
}

int gcd(int a, int b)
{
return ( b == 0 ? a : gcd(b, a % b) );
}

____________________________________________________________________

Enter two positive integers: 36 6

Divisors of 36 that do not divide 6 are:
30
The gcd of 30 and 6 is 6
24
The gcd of 24 and 6 is 6
18
The gcd of 18 and 6 is 6
12
The gcd of 12 and 6 is 6
6
The gcd of 6 and 6 is 6
0
The gcd of 6 and 0 is 6
Press any key to continue . . .

----------------------------------------------------------------------

I put the final output inside the while loop because I wanted to see exactly what my program was doing, but I plan on moving it back outside of the loop later. As a side note, I want the final output to contain the first two numbers entered, and not the results of the equation. ("The gcd of 36 and 6 is 6" instead of what it currently says.) I know using "static" won't do what I want it to, so how do I reset the variables to their former values before outputing them?
Last edited on
:= is used in languages like Pascal, Delphi as an assignment operator. It has no use in C++

http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B
Last edited on
Edit: Question is still open for answers.

Oh. Thank you for your help! In that case, how do I get my program to output what I want it to? As stated in the program, I want it to output the divisors of "b" that do not divide "a". I've tried everything I can think of, but I'm only in an intro to c++ class. Once again, thank you for your help!
Last edited on
There are a couple of ways. One way is to find all of the divisors of b, and for
each one, see if it divides a. How can you find all of the divisors of b?

Another way is the following: if the Greatest Common Divisor of A and B is G,
then the common factors of both A and B are the prime factors of G.

Either approach will require a little more than just the gcd algorithm.

It would have to be some sort of "for" or "while" loop, right? Hm. I'll have to try that out. Thank you so much! I was completely stuck! I'll post when it works.
Last edited on
Topic archived. No new replies allowed.