Program to discover GCD or HCF of two numbers
GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the biggest number that partitions them two.
36=2x2x3x3
60=2x2x3x5
GCD=Multiplication of regular elements
=2x2x3
=12
Fundamental Euclidean Algorithm for GCD
The calculation depends on beneath realities.
On the off chance that we subtract more modest number from bigger (we diminish bigger number), GCD doesn't change. So in the event that we continue subtracting over and over the bigger of two, we wind up with GCD.
Presently rather than subtraction, on the off chance that we isolate more modest number, the calculation stops when we discover leftover portion 0.
Java Program to Find GCD of two Numbers
In this program, you'll figure out how to discover GCD of two numbers in Kotlin. This is finished by utilizing for and keeping in mind that circles with the assistance of if else articulations.
The HCF or GCD of two whole numbers is the biggest whole number that can precisely separate the two numbers (without a leftover portion).
Illustration 1: Find GCD of two numbers utilizing for circle and if articulation
public class GCD {
public static void main(String[] args) {
int n1 = 81, n2 = 153, gcd = 1;
for(int i = 1; i <= n1 && i <= n2; ++i)
{
// Checks if i is factor of both integers
if(n1 % i==0 && n2 % i==0)
gcd = i;
}
System.out.printf("G.C.D of %d and %d is %d", n1, n2, gcd);
}
}
When you run the program, the output will be:
G.C.D of 81 and 153 is 9
Here, two numbers whose GCD are to be found are stored in n1 and n2 respectively.
Then, a for loop is executed until i is less than both n1 and n2. This way, all numbers between 1 and smallest of the two numbers are iterated to find the GCD.
If both n1 and n2 are divisble by i, gcd is set to the number. This goes on until it finds the largest number (GCD) which divides both n1 and n2 without remainder.
We can also solve this problem using a while loop as follows:
Example 2: Find GCD of two numbers using while loop and if else statement
public class GCD {
public static void main(String[] args) {
int n1 = 81, n2 = 153;
while(n1 != n2)
{
if(n1 > n2)
n1 -= n2;
else
n2 -= n1;
}
System.out.println("G.C.D = " + n1);
}
}
When you run the program, the output will be:
G.C.D = 9
This is a superior method to discover the GCD. In this strategy, littler number is subtracted from the bigger whole number, and the outcome is relegated to the variable holding bigger whole number. This procedure is proceeded until the point when n1 and n2 are equivalent.
The over two projects fills in as proposed just if the client enters positive numbers. Here's a little alteration of the second case to discover the GCD for both positive and negative whole numbers.
Illustration 3: GCD for both positive and negative numbers
public class GCD {
public static void main(String[] args) {
int n1 = 81, n2 = -153;
// Always set to positive
n1 = ( n1 > 0) ? n1 : -n1;
n2 = ( n2 > 0) ? n2 : -n2;
while(n1 != n2)
{
if(n1 > n2)
n1 -= n2;
else
n2 -= n1;
}
System.out.println("G.C.D = " + n1);
}
}
When you run the program, the output will be:
G.C.D = 9
0 comments:
Post a Comment