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 as 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.

  • Construcotr name is as similar name as class
  • It does have any return type.

Also Read: How to Convert CSV to TXT in Java?

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.

  1. Find GCD of two numbers using Default Construtor
  2. Find GCD of two numbers using Parameterized Construtor

Simple Code Logic

for (int i = 1; i <= num1 && i <= num2; ++i) { // Check whether both numbers are divisible or not if (num1 % i == 0 && num2 % i == 0) gcd = i; }
Code language: JavaScript (javascript)

1. Find GCD of two numbers using Default Construtor

We can find GCD of two numbers using Constructor like Default Constructor in which we will define simple logic to find GCD using for loop.

Let’s see the complete program now.

Program to find GCD of Two Numbers Using Default Constructor

/** * Java program to Find GCD of Two Numbers Using Default Constructor */ package cf.java.logical; import java.util.Scanner; public class FindGCDUsingConstructor { // Declare both numbers and gcd int num1, num2, gcd; // Declare scanner class to accept numbers from user Scanner sc = new Scanner(System.in); public FindGCDUsingConstructor() { // Accept first both numbers from user System.out.print("Enter first number: "); num1 = sc.nextInt(); System.out.print("Enter second number: "); num2 = sc.nextInt(); // Initially set gcd to 1 gcd = 1; for (int i = 1; i <= num1 && i <= num2; ++i) { // Check whether both numbers are divisible or not if (num1 % i == 0 && num2 % i == 0) gcd = i; } // Display GCD of two numbers System.out.println("GCD of " + num1 + " & " + num2 + " is " + gcd); } public static void main(String[] args) { // Create object of class which will call the constructor automatically FindGCDUsingConstructor findGCD = new FindGCDUsingConstructor(); } }
Code language: PHP (php)

Output:

Enter first number: 56 Enter second number: 88 GCD of 56 & 88 is 8

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 Construtor

We can find GCD of two numbers using Constructor like Parameterized Constructor in which we will define simple logic to find GCD using for loop.

Let’s see the complete program now.

Program to find GCD of Two Numbers Using Parameterized Constructor

/** * Java program to Find GCD of Two Numbers Using Parameterized Constructor */ package cf.java.logical; import java.util.Scanner; public class FindGCDUsingParameterizedConstructor { // Declare a variable for gcd int gcd; // Declare scanner class to accept numbers from user Scanner sc = new Scanner(System.in); public FindGCDUsingParameterizedConstructor(int num1, int num2) { // Initially set gcd to 1 gcd = 1; for (int i = 1; i <= num1 && i <= num2; ++i) { // Check whether both numbers are divisible or not if (num1 % i == 0 && num2 % i == 0) gcd = i; } // Display GCD of two numbers System.out.println("GCD of " + num1 + " & " + num2 + " is " + gcd); } public static void main(String[] args) { // Create object of class which will call the constructor automatically FindGCDUsingParameterizedConstructor findGCD = new FindGCDUsingParameterizedConstructor(56, 88); } }
Code language: PHP (php)

Output:

GCD of 56 & 88 is 8

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.

FAQs

Is there any GCD function in Java?

Yes, we have one gcd( ) method of BigInteger class which we can use to find GCD of two numbers.
Syntax of java.math.BigInteger.gcd() method:
public BigInteger gcd(BigInteger val)

Can we find the GCD of two numbers by using Scanner?

Yes, we can use the Scanner class to get two numbers from the user and by using these two numbers, we can find the GCD also.
In the example, we have already used the Scanner class. You can refer to the Default Constructor example to know more about this.

Can we use Parameterized Constructor to find the GCD of two numbers?

Yes, We can use Parameterized Constructor to find the GCD of numbers and while creating objects, we can also provide the numbers using parameters.

What is the GCD example?

GCD (Greatest Common Divisor) is the highest or greatest positive common divisor of two numbers. For example, GCD of 56 and 88 is 8, and GCD of 12 and 8 is 4.

Leave a Comment