By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
 432,009 Members | 1,487 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,009 IT Pros & Developers. It's quick & easy.

# Euclids Algorithm code problem

 P: n/a Hi, I am having trouble with my code. My program compiles and runs correctly if the value for X is less than Y. If the value of X is greater than Y it gives me a GCD of one because it is still executing the statements for the If statement. I know it has to do with the scope of my if statement. Does anyone have any suggestions? Expand|Select|Wrap|Line Numbers #include     using namespace std ;   int main()  {        int x ;     int y ;     int temp ;     int remainder ;       // read in the two integers       cout << endl ;     cout << "Enter the first number (integer) : " ;      cin >> x ;     cout << "Enter the second number (integer) : " ;      cin >> y ;       //echo inputs       cout << "Input numbers are: " << x << " , " << y << endl ;    { // exchange values of x and y           if (x < y)         temp = x ;      x = y ;      y = temp ;        }       /* At this point we will always have x >= y */             while (x % y != 0)         {                   x = y ;          y = remainder ;          remainder = (x % y) ;         }       // display the result     cout << endl ;     cout << "The GCD is: " << y << endl ;     system("PAUSE");     return (0); // terminate with success }   Oct 30 '10 #1
Share this Question
1 Reply

 Expert Mod 5K+ P: 8,916 Put braces { } round the code that should be part of the if statement block. Oct 31 '10 #2

### Post your reply

Sign in to post your reply or Sign up for a free account.