1. Introduction
In this tutorial, We will learn how to calculate the GCD for a given two numbers. GCD means Greatest Common Divisor.
The GCD of two numbers is the largest positive integer number that divides both the numbers without leaving any remainder (remainder should be 0). In some of the cases, GCD is called the Highest Common Factor (HCF).
Let us take an example GCD of 60, 90 is 30.
Factors of a number 60 : 2 * 2 * 3 * 5
Factors of a number 90 : 2 * 3 * 3 * 5
Common unique factors are: 2, 3, 5
Multiplication of common factors = 2 * 3 * 5 = 30
This can be done in two approaches.
A) Running the loop till the minimum of two numbers
B) GCD finding using the subtraction method.
These two methods are implemented without using a recursive approach.
2. Running a for loop till a minimum of two numbers
In this approach, we will first find the minimum of two numbers using Math.min(a, b) method which returns the min number and will run the for loop till min number.
package com.java.w3schools.blog.java.program.to; import java.util.Scanner; /** * * Java Program to Find GCD of Two Numbers using and Running a for loop till * minimum of two numbers * * @author JavaProgramTo.com * */ public class GCDForLoop { public static void main(String[] args) { System.out.println("Enter a first number : "); Scanner scanner = new Scanner(System.in); int first = scanner.nextInt(); System.out.println("Enter a second number : "); int second = scanner.nextInt(); // executing a for loop till min of two numbers. int min = Math.min(first, second); int gcd = 1; for (int i = 1; i <= min; i++) { if (first % i == 0 && second % i == 0) { gcd = i; } } System.out.println("GCD of two numbers (" + first + ", " + second + ") is : " + gcd); } }
Output:
Enter a first number : 60 Enter a second number : 90 GCD of two numbers (60, 90) is : 30
3. Using Subtraction method
We will be using a while loop with subtraction logic. This is a very simple logic and efficient one. Here, we will be subtracting from the larger number and smaller number untill both numbers becomes the same. In the end, the second number will be the minimum number which will be the GCD of those two numbers.
Printing prime numbers with while loop example program
package com.java.w3schools.blog.java.program.to; import java.util.Scanner; /** * * Java Program to Find GCD of Two Numbers Using Subtraction method * * @author JavaProgramTo.com * */ public class GCDSubstractMethod { public static void main(String[] args) { System.out.println("Enter a first number : "); Scanner scanner = new Scanner(System.in); int first = scanner.nextInt(); System.out.println("Enter a second number : "); int second = scanner.nextInt(); while (first != second) { if (first > second) { first = first - second; } else { second = second - first; } } System.out.println("GCD : " + second); } }
Output:
Enter a first number : 50 Enter a second number : 120 GCD : 10
4. Possible Exceptions
If you do not handle the for loop properly and if you do divide the number with 0 then you will end up in the following exception.
Exception in thread "main" java.lang.ArithmeticException: / by zero at com.java.w3schools.blog.java.program.to.GCDForLoop.main(GCDForLoop.java:30)
5. Conclusion
In this article, We have seen how to find the GCD of two numbers in java using two methods. The first approach is to run a for loop till a minimum of two numbers and the second approach is using while loop with subtract method.
The second method is most suitable with higher number input values and most efficient method.
No comments:
Post a Comment
Please do not add any spam links in the comments section.