0

I have to write a C++ code that computes the greatest common divisor of two numbers.
When Declaring x, y, temp, remainder as a Integer is this right? Or can someone help me in the right direction.

int x = 0;
    int y = 0;

    if (x < y) then
       
    cout << temp = x; 
	cin >> x = y; 
    cout << y = temp 	
	
	if else remainder = x % y
3
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by mrboolf
0

I assume the numbers are x and y?
You set them to zero and then you do an if
saying if(0<0) then...
Do you think this can work?

First cin x and cin y then call a function say int GCD(x,y) to compute your gcd and cout that. And btw x and y can ONLY be integers! The variable temp is never needed.

0

I'd suggest you to take a look at the Euclidean Algorithm - I don't post links because there's often an example of implementation which could offer some copy-paste temptation...
... not that you couldn't find'em if you wanted to, it's such a common topic...
I guess it's your responsability to be responsible :)

However, let me suggest you to study the syntax first... because the code you posted is far from correct, syntactically apart from logically.

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.