0

Solving the greatest common divisor using C++ and the Euclid method

Below is my code:

#include "stdafx.h"
#include "genlib.h"
#include "simpio.h"
#include "math.h"

int _tmain(int argc, _TCHAR* argv[])
{
    int num1,num2,rem;

    printf("Enter 1st number: ");
    num1=GetInteger();
    printf("Enter 2nd number: ");
    num2=GetInteger();
    rem=num1%num2;
    ;if(rem==0)
        {
            printf("The GCD of %d and %d is %d\n",num1,num2,num2);
        }
     else
        {num1=num2;
         num2=rem;
         while (1==1)
         {
             if (rem==0)
                 {
                     printf("The GCD of %d and %d is %d\n",num1,num2,num2);
                 }
             else
                 {
                     num1=num2;
                     num2=rem;
                 }
          }
         }  
}

Please tell me what I did wrong, because the above code doesn't work for all numbers.

thank you

Edited by mike_2000_17: Fixed formatting

3
Contributors
2
Replies
5
Views
7 Years
Discussion Span
Last Post by daviddoria
0

Your while() condition in the first else statement is (1==1) which is always true.
It might be why you are having the problem.

0

Please use code tags when posting code. Also, which numbers are not working correctly? You should be as detailed as possible when posting your problems so people here can help you as efficiently as possible.

David

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.