DEV Community

Poornima Ravi
Poornima Ravi

Posted on

LCM & GCD of two numbers

#LCM of two numbers
Num1 = int(input('Enter the Number 1:'))
Num2 = int(input('Enter the Number 2:'))
def evaluateN(Num1,Num2):
    if Num1>Num2:
        N= Num1
    else:
        N= Num2  
    return N
LCM = 1
i = 2
N = evaluateN(Num1,Num2)
if Num1>1 or Num2>1: 
    print(Num1,Num2)
    while i<=N:
        if Num1%i ==0 and Num2%i ==0:
            Num1 = Num1//i
            Num2 = Num2//i
            LCM = LCM * i
            print('i:',i, '|',Num1,Num2)
            i=2
        elif Num1%i ==0 and Num2%i !=0:
            Num1 = Num1//i
            LCM = LCM * i
            print('i:',i,'|', Num1,Num2)
            i=2
        elif Num1%i !=0 and Num2%i ==0:
            Num2 = Num2//i
            LCM = LCM * i
            print('i:',i,'|', Num1,Num2)
            i=2
        else:
            i+=1   
        N = evaluateN(Num1,Num2)   
    print('LCM :', LCM)
elif Num1==Num2:
    print('LCM :',LCM)
else:
    print('Enter Valid Num')



#GCD of two Numbers
Num1 = int(input('Enter the Number 1:'))
Num2 = int(input('Enter the Number 2:'))
def evaluateN(Num1,Num2):
    if Num1>Num2:
        N= Num1
    else:
        N= Num2  
    return N
GCD = 1
i = 2
N = evaluateN(Num1,Num2)
if Num1>1 or Num2>1: 
    print(Num1,Num2)
    while i<=N:
        if Num1%i ==0 and Num2%i ==0:
            Num1 = Num1//i
            Num2 = Num2//i
            GCD = GCD * i
            print('i:',i, '|',Num1,Num2)
            i=2
        else:
            i+=1   
        N = evaluateN(Num1,Num2)   
    print('GCD:', GCD)
elif Num1==Num2:
    print('GCD:',GCD)
else:
    print('Enter Valid Num')

Enter fullscreen mode Exit fullscreen mode

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more