Find GCD of Two Numbers

Ques: Problem: To find GCD of two number.​

NOTE:- Please comment down the code in other languages as well below –

23 comments on “Find GCD of Two Numbers”


  • Abimanyu

    def gcd_iter(u, v):
    while v:
    t = u
    u = v
    v = t % v
    return abs(u)

    n = 3
    m = 6
    result = gcd_iter(n, m)
    print(result)


  • MOGIT

    #include

    int gcd(int a, int b);
    int main()
    {
    int n,m,hcf;
    printf(“Enter the two value:”);
    scanf(“%d%d”,&n,&m);
    hcf=gcd(n,m);
    printf(“hcf=%d”,hcf);
    return 0;
    }
    int gcd(int a,int b)
    {
    if(b==0)
    return a;
    else
    return gcd(b,a%b);
    }


  • KAVIN

    #include
    void main()
    {
    int n1,n2,n3,i,gcd;
    printf(“Enter 3 numbers : “);
    scanf(“%d %d %d”, &n1, &n2, &n3); for(i=1;i<=n1&&i<=n2&&i<=n3;i++)
    {
    if(n1%i==0 &&n2%i==0 &&n3%i==0)
    {
    gcd=i;
    }
    }
    printf("GCD of %d & %d & %d is %d",n1,n2,n3,gcd);
    }


  • reshma

    In C prog
    #include
    void main()
    {
    int n1,n2,n3,i,gcd;
    printf(“Enter 3 numbers : “);
    scanf(“%d %d %d”, &n1, &n2, &n3);

    for(i=1;i<=n1&&i<=n2&&i<=n3;i++)
    {
    if(n1%i==0 &&n2%i==0 &&n3%i==0)
    {
    gcd=i;
    }
    }
    printf("GCD of %d & %d & %d is %d",n1,n2,n3,gcd);
    }


  • Deep

    python
    class solution():
    def getGCD(self,m,n):
    while(n):
    t = m
    m = n
    n = t%n
    if m<0:
    return -m
    else:
    return m

    m = int(input())
    n =int(input())
    p = solution()
    print(p.getGCD(m,n))


  • sayandip

    #GCD and LCM of two numbers
    m,n=map(int,input().split())
    mul=m*n
    def gcd(x,y):
    if(y==0):
    return(x)
    else:
    return(gcd(y,x%y))
    z=gcd(m,n)
    lcm=mul/z
    print(“GCD is “+str(z))
    print(“LCM is “+str(lcm))