java

find-gcd()

Parameters: int a, int b

Two integers for which the greatest common divisor is to be calculated

Returns: The greatest common divisor of the two provided integers

The function find-gcd calculates the greatest common divisor (GCD) of two input numbers, providing an essential functionality in number theory and in computer science.

Variables
Conditionals
Recursion
Easy dificulty

Writing a Java Function to Find the Greatest Common Divisor

Greetings, programmer! Welcome to this blog post. In the steps below, we'll guide you through the process of programming a function to find the Greatest Common Divisor (GCD) in Java. This function is fundamental in computer science, and understanding it will help enhance your problem-solving skills. Stay cool, curious and code on!

Step 1: Understand what is GCD

The Greatest Common Divisor(GCD) of two integers is the largest positive integer that divides both the numbers without leaving a remainder. This concept is used frequently in mathematical computations and also in various programming code logic. In Java, we will create a function findGCD that takes two integer parameters and returns their GCD.

Step 2: Create the Basic Structure

To start with, we create a new function findGCD. This function accepts two parameters, namely num1 and num2. These are the two numbers for which we are supposed to find the GCD.

public static int findGCD(int num1, int num2) {
  // code logic will go in here   
}

Step 3: Implement GCD Logic Using Euclid's Algorithm

Euclid's Algorithm is a popular method to calculate the GCD of two numbers. The main idea of this algorithm is that the GCD of two numbers remains the same when the larger number is replaced by its difference with the smaller number. Continue this process until both numbers become equal, which would be our GCD.

public static int findGCD(int num1, int num2) {  
    if(num1 == 0)   
       return num2;  
    return findGCD(num2%num1, num1);
}

Step 4: Handle Edge Cases

There might be cases where the user inputs a negative number. However, GCD can only be a positive number. To handle this, we take the absolute value of inputs before processing them in our findGCD function.

public static int findGCD(int num1, int num2) {
    num1 = Math.abs(num1); 
    num2 = Math.abs(num2); 
    if(num1 == 0)   
       return num2;  
    return findGCD(num2%num1, num1);  
}

Step 5: Test the findGCD function

After you have implemented the function, it is important to test it with different set of inputs to ensure it's working as expected. Here is the completed function with some test cases.

public class Main {
    public static void main(String[] args) {
        System.out.println(findGCD(60, 48)); // should return 12
        System.out.println(findGCD(-60, 48)); // should return 12
        System.out.println(findGCD(101, 103)); // should return 1
    }
    public static int findGCD(int num1, int num2) {
        num1 = Math.abs(num1); 
        num2 = Math.abs(num2); 
        if(num1 == 0)   
           return num2;  
        return findGCD(num2%num1, num1);  
    }
}

Learn function in:

Greatest Common Divisor

Determines the largest number that divides two numbers without a remainder

Learn more

Mathematical principle

The GCD of two input numbers is the largest number that divides both of the numbers without leaving a remainder. The Euclidean algorithm, an efficient method for computing the GCD, is often used in programming to implement this functionality. This algorithm is based on the principle that the GCD of two numbers also divides their difference. Thus, in a recursive process, the original pair of numbers can be reduced to smaller pairs, until the pair is such that one number divides the other.

Learn more