Home Programs Program to find GCD of two numbers – Technic Dude

Program to find GCD of two numbers – Technic Dude

0
10

Program to find GCD of two numbers in c language , java language and python language by technic dude . GDD full form is The Greatest Common Divisor (GCD) 

<img width=

program to find GCD of two numbers in C


#include <stdio.h>
 
// Recursive function to return gcd of a and b
int gcd(int a, int b)
{
    // Everything divides 0
    if (a == 0)
       return b;
    if (b == 0)
       return a;
 
    // base case
    if (a == b)
        return a;
 
    // a is greater
    if (a > b)
        return gcd(a-b, b);
    return gcd(a, b-a);
}
 
// Driver program to test above function
int main()
{
    int a = 98, b = 56;
    printf("GCD of %d and %d is %d ", a, b, gcd(a, b));
    return 0;
}






Output

GCD of 98 and 56 is 14

program to find GCD of two numbers in JAVA

class Test
{
    // Recursive function to return gcd of a and b
    static int gcd(int a, int b)
    {
        // Everything divides 0
        if (a == 0)
          return b;
        if (b == 0)
          return a;
      
        // base case
        if (a == b)
            return a;
      
        // a is greater
        if (a > b)
            return gcd(a-b, b);
        return gcd(a, b-a);
    }
     
    // Driver method
    public static void main(String[] args)
    {
        int a = 98, b = 56;
        System.out.println("GCD of " + a +" and " + b + " is " + gcd(a, b));
    }
}



Output



GCD of 98 and 56 is 14

program to find GCD of two numbers in Python

def gcd(a,b):
     
    
    if (b == 0):
         return a
    return gcd(b, a%b)
 

a = 98
b = 56
if(gcd(a, b)):
    print('GCD of', a, 'and', b, 'is', gcd(a, b))
else:
    print('not found')

Output

GCD of 98 and 56 is 14

READ MORE

NO COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here