GCD of Two Numbers

Medium Numbers
Find the greatest common divisor (HCF) of two numbers using the Euclidean algorithm.

Sample input

48, 18

Sample output

6

Solution

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

print(gcd(48, 18))
function gcd(a, b) {
  while (b) {
    [a, b] = [b, a % b];
  }
  return a;
}

console.log(gcd(48, 18));
public class Main {
    static int gcd(int a, int b) {
        while (b != 0) {
            int t = b;
            b = a % b;
            a = t;
        }
        return a;
    }

    public static void main(String[] args) {
        System.out.println(gcd(48, 18));
    }
}
fun gcd(a: Int, b: Int): Int {
    var x = a
    var y = b
    while (y != 0) {
        val t = y
        y = x % y
        x = t
    }
    return x
}

fun main() {
    println(gcd(48, 18))
}
func gcd(_ a: Int, _ b: Int) -> Int {
    var x = a, y = b
    while y != 0 {
        let t = y
        y = x % y
        x = t
    }
    return x
}

print(gcd(48, 18))
int gcd(int a, int b) {
  while (b != 0) {
    int t = b;
    b = a % b;
    a = t;
  }
  return a;
}

void main() {
  print(gcd(48, 18));
}
#include <iostream>
using namespace std;

int gcd(int a, int b) {
    while (b != 0) {
        int t = b;
        b = a % b;
        a = t;
    }
    return a;
}

int main() {
    cout << gcd(48, 18) << endl;
    return 0;
}
#include <stdio.h>

int gcd(int a, int b) {
    while (b != 0) {
        int t = b;
        b = a % b;
        a = t;
    }
    return a;
}

int main() {
    printf("%d\n", gcd(48, 18));
    return 0;
}