Find Greatest Common Divisor

package org.wesome.dsalgo;

import lombok.AllArgsConstructor;
import lombok.Data;

public class GreatestCommonDivisor {
    public static int greatestCommonDivisor(int num1, int num2) {
        int gcf = 1;
        for (int i = 1; i <= num1 && i <= num2; i++) {
            if (num1 % i == 0 && num2 % i == 0)
                gcf = i;
        }
        return gcf;
    }
}
package org.wesome.dsalgo;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class GreatestCommonDivisorTest {
    @Test
    void greatestCommonDivisor1() {
        assertEquals(4, GreatestCommonDivisor.greatestCommonDivisor(16, 12));
    }

    @Test
    void greatestCommonDivisor2() {
        assertEquals(4, GreatestCommonDivisor.greatestCommonDivisor(4, 12));
    }

    @Test
    void greatestCommonDivisor3() {
        assertEquals(2, GreatestCommonDivisor.greatestCommonDivisor(10, 2));
    }
}
plugins {
    id 'java'
    id "io.freefair.lombok" version "6.2.0"
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
}

test {
    useJUnitPlatform()
}

 

follow us on