Skip to main content
Added spacing around operators
Source Link
nikk
  • 7
  • 1
#--input
a=a = int(input("Enter a number: "))
print() # new line
b=b = int(input("Enter a number: "))
print() # new line

#--check/make a > b only once
if (b>ab > a):
    a,b=bb = b,a
    
#--one simple loop for GCD 
while(a%b !=0= 0):
    x,a=aa = a,b
    b=x%bb = x%b

#--If one or both values are negative
if(b<0b < 0):
    b*=b *= -1

print ("GCD=""GCD = ", b)

Try this. It has one small loop for GCD. And -ve value correction. I've added comments to describe the code.

#--input
a= int(input("Enter a number: "))
print() # new line
b= int(input("Enter a number: "))
print() # new line

#--check/make a > b only once
if (b>a):
    a,b=b,a
    
#--one simple loop for GCD 
while(a%b!=0):
    x,a=a,b
    b=x%b

#--If one or both values are negative
if(b<0):
    b*=-1

print ("GCD=", b)

Try this. It has one small loop for GCD. And -ve value correction. I've added comments to describe the code.

#--input
a = int(input("Enter a number: "))
print() # new line
b = int(input("Enter a number: "))
print() # new line

#--check/make a > b only once
if (b > a):
    a,b = b,a
    
#--one simple loop for GCD 
while(a%b != 0):
    x,a = a,b
    b = x%b

#--If one or both values are negative
if(b < 0):
    b *= -1

print ("GCD = ", b)

Try this. It has one small loop for GCD. And -ve value correction. I've added comments to describe the code.

Edited Print. and variable swap
Source Link
nikk
  • 7
  • 1
#--input
a= int(input("Enter a number: "))
print() # new line
b= int(input("Enter a number: "))
print() # new line

#--check/make a > b only once
if (b>a):
    r=a
    a=b
    b=ra,b=b,a
    
#--one simple loop for GCD 
while(a%b!=0):
    x=a
    a=bx,a=a,b
    b=x%b

#--If one or both values are negative
if(b<0):
    b*=-1

print ("GCD=", b)

Try this. It has one small loop for GCD. And -ve value correction. I've added comments to describe the code.

#--input
a= int(input("Enter a number: "))
print # new line
b= int(input("Enter a number: "))
print # new line

#--check/make a > b only once
if (b>a):
    r=a
    a=b
    b=r
    
#--one simple loop for GCD 
while(a%b!=0):
    x=a
    a=b
    b=x%b

#--If one or both values are negative
if(b<0):
    b*=-1

print "GCD=", b

Try this. It has one small loop for GCD. And -ve value correction. I've added comments to describe the code.

#--input
a= int(input("Enter a number: "))
print() # new line
b= int(input("Enter a number: "))
print() # new line

#--check/make a > b only once
if (b>a):
    a,b=b,a
    
#--one simple loop for GCD 
while(a%b!=0):
    x,a=a,b
    b=x%b

#--If one or both values are negative
if(b<0):
    b*=-1

print ("GCD=", b)

Try this. It has one small loop for GCD. And -ve value correction. I've added comments to describe the code.

added 4 characters in body
Source Link
nikk
  • 7
  • 1
#--input
a= int(input("Enter a number: "))
print # new line
b= int(input("Enter a number: "))
print # new line

#--check/make a > b only once
if (b>a):
    r=a
    a=b
    b=r
    
#--one simple loop for GCD 
while(a%b!=0):
    x=a
    a=b
    b=x%b

#--If one or both values are negative
if(b<0):
    b*=-1

print "GCD=", b

Try this. It has one small loop for GCD. And -ve value correction. I've added comments to describe the code.

#--input
a= int(input("Enter a number: "))
print # new line
b= int(input("Enter a number: "))
print # new line

#--check/make a > b only once
if (b>a):
    r=a
    a=b
    b=r
    
#--one simple loop for GCD 
while(a%b!=0):
    x=a
    a=b
    b=x%b

#--If one or both values are negative
if(b<0):
b*=-1

print "GCD=", b

Try this. It has one small loop for GCD. And -ve value correction. I've added comments to describe the code.

#--input
a= int(input("Enter a number: "))
print # new line
b= int(input("Enter a number: "))
print # new line

#--check/make a > b only once
if (b>a):
    r=a
    a=b
    b=r
    
#--one simple loop for GCD 
while(a%b!=0):
    x=a
    a=b
    b=x%b

#--If one or both values are negative
if(b<0):
    b*=-1

print "GCD=", b

Try this. It has one small loop for GCD. And -ve value correction. I've added comments to describe the code.

Added comments in code
Source Link
nikk
  • 7
  • 1
Loading
Added -ve value correction.
Source Link
nikk
  • 7
  • 1
Loading
Added -ve value correction.
Source Link
nikk
  • 7
  • 1
Loading
Added comments and corrected code
Source Link
nikk
  • 7
  • 1
Loading
Post Undeleted by nikk
Post Deleted by nikk
Source Link
nikk
  • 7
  • 1
Loading