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

Recommended Answers

All 2 Replies

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.