Best 2 Ways To Find GCD Of Two Numbers Using Constructor In Java
We can find GCD of two numbers using constructor in Java, like default Constructor or Parameterized constructor. Let’s see the complete simple programs and understand them step-by-step.
Let’s understand some simple terms like what is GCD, what is the constructor and how can we use them to find the GCD of two numbers.
What GCD Stands for?
“GCD is the highest or greatest positive common divisor of two numbers. It can’t be negative or zero as the least common divisor is 1 always.”
- GCD stands for Greatest Common Divisor.
- It is also called HCF (Highest Common Factor).
What is Constructor in Java?
Constructor in Java is a method which name is similar to the class name and is called when an instance of an object is created.
- Constructor name is as similar name as the class
- It does have any return type.
How to Find GCD of Two Numbers Using Constructor?
We can find the GCD of two numbers using Constructor and for loop. We can use logic to find GCD inside of Constructor itself.
Let’s see the types of constructors that we can use to find the GCD of two numbers.
- Find GCD of two numbers using Default Constructor
- Find GCD of two numbers using Parameterized Constructor
Simple Code Logic
1. Find GCD of two numbers using Default Constructor
We can find GCD of two numbers using Constructor like Default Constructor in which we will define simple logic to find GCD using.
Let’s see the complete program now.
Program to find GCD of Two Numbers Using Default Constructor
Complete Program: https://codingface.com/find-gcd-of-two-numbers-using-constructor-in-java/
Explanation:
In this program, we have used Default Constructor to find the GCD of numbers.
In the main( ) function, when we will create the object of the class, it will automatically call the Default Constructor ( public FindGCDUsingConstructor( ) ), which contains the logic to find the GCD of two numbers.
First, we have asked the user to provide two numbers which we have a store by using num1 and num2 variables. Initially, we have set the gcd value to 1.
Inside for loop, we have checked whether both numbers num1 and num2 were divisible by the “i” value or not. If it will be divisible then we will set the gcd value to “i”.
Finally, when the for loop iteration will be completed, we have displayed the GCD of two given numbers to the user.
2. Find GCD of two numbers using Parameterized Constructor
We can find GCD of two numbers using Constructor like Parameterized Constructor in which we will define simple logic to find GCD using.
Let’s see the complete program now.
Program to find GCD of Two Numbers Using Parameterized Constructor
Complete Program: https://codingface.com/find-gcd-of-two-numbers-using-constructor-in-java/
Explanation:
In this program, we have used Parameterized Constructor to find the GCD of numbers.
In the main( ) function, As we have passed arguments while created an object of the class, So it will automatically call the Parameterized Constructor ( public FindGCDUsingConstructor(int num1, int num2) ), which contains the logic to find the GCD of two numbers.
Initially, we have set the gcd value to 1. Inside for loop, we have checked whether both numbers num1 and num2 were divisible by the “i” value or not. If it will be divisible then we will set the gcd value to “i”.
Finally, when the for loop iteration will be completed, we have displayed the GCD of two given numbers to the user.
Codingface Last Words
There are several methods to find the GCD of two numbers. To find GCD of two numbers using constructor, we can use either Default Constructor or Parameterized Constructor. In both cases, we can use the above same simple logic to find GCD and only the difference is parameters that we have used in Parameterized Constructor.
Originally published at https://codingface.com on October 13, 2021.