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”


  • plohiya2000

    code in python:-
    def GCDnum(n,m):
    while(n):
    t = m
    m = n
    n = t % n

    return m if m>0 else -m

    x = 3
    y = 6
    print(GCDnum(x,y))


  • Hityasha

    JAVA PROGRAM….
    import java.util.Scanner;
    class Test{
    public static void main(String[] args){
    Scanner sc=new Scanner(System.in);
    int x=sc.nextInt();
    int y=sc.nextInt();
    System.out.println(“GCD is:” findGCD(x,y));
    }
    static int findGCD(int x, int y)
    {
    int r=0, a, b;
    a=(x>y)?x:y; //a is greater
    b=(x<y)? x: y; //b is smaller
    r=b;
    while(a%b !=0)
    {
    r=a%b;
    a=b;
    b=r;
    }
    return r;
    }
    }


  • swati

    #include

    int HCF_cal(int a, int b)
    {
    int HCF;
    for(HCF = a = 1; HCF–)
    {
    if(a%HCF==0 && b%HCF==0)
    {
    break;
    }
    }

    return HCF;
    }

    int main()
    {
    int n1, n2, Result;
    scanf(“%d %d”, &n1, &n2);

    Result = HCF_cal(n1, n2);

    printf(“HCF(%d, %d) = %d”, n1, n2, Result);
    }


  • swati

    #include

    int HCF_cal(int a, int b)
    {
    int HCF;
    for(HCF = a = 1; HCF–)
    {
    if(a%HCF==0 && b%HCF==0)
    {
    break;
    }
    }

    return HCF;
    }

    int main()
    {
    int n1, n2, Result;
    scanf(“%d %d”, &n1, &n2);

    Result = HCF_cal(n1, n2);

    printf(“HCF(%d, %d) = %d”, n1, n2, Result);
    }


  • mahesh

    in python ——-
    import math
    num1 = int(input(“enter number 1”))
    num2 = int(input(“enter number 2”))

    print(math.gcd(num1,num2))


  • Sahithi chowdary

    python

    import math
    num1=int(input(“enter the number”))
    num2=int(input(“enter the number2”))
    print()
    print(math.gcd(num1,num2))


  • RAINA

    #include
    int main()
    {
    int m,n,i,gcd=1,min;
    scanf(“%d%d”,&n,&m);
    if(n>=m)
    {
    min=m;
    }
    else
    {
    min=n;
    }
    for(i=1;i<=min;i++)
    {
    if((n%i==0)&&(m%i==0))
    {
    gcd=i;
    }
    }
    printf("%d",gcd);

    return 0;
    }


  • Jayakumar Jk

    #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);
    }


    • Avinash

      #include
      using namespace std;
      gcd(int a,int b)
      {
      if(a==0)
      return b;
      return gcd(b%a,a);
      }
      int main()
      {
      int a,b;
      cin>>a>>b;
      cout<<gcd(a,b);
      }