Develop a Java program to find the GCD (Greatest Common Divisor) of two numbers.Input FormatTwo integers a and b separated by a space.ConstraintsThe absolute values of a and b will be less than or equal to 10^9. a and b are non-negative integers.Output FormatA single integer representing the GCD of a and b.Sample Input 012 18Sample Output 06Contest ends in an hourSubmissions: 0Max Score: 5Difficulty: EasyRate This Challenge: More Java 151import java.io.*;2import java.util.*;34public class Solution {56 public static void main(String[] args) {7 /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */8 }9}
Question
Develop a Java program to find the GCD (Greatest Common Divisor) of two numbers.Input FormatTwo integers a and b separated by a space.ConstraintsThe absolute values of a and b will be less than or equal to 10^9. a and b are non-negative integers.Output FormatA single integer representing the GCD of a and b.Sample Input 012 18Sample Output 06Contest ends in an hourSubmissions: 0Max Score: 5Difficulty: EasyRate This Challenge: More Java 151import java.io.;2import java.util.;34public class Solution {56 public static void main(String[] args) {7 /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */8 }9}
Solution
Sure, here is a Java program that calculates the GCD of two numbers:
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
System.out.println(gcd(a, b));
}
private static int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
}
Here's how it works:
-
The
mainmethod reads two integersaandbfrom the standard input using aScannerobject. -
It then calls the
gcdmethod withaandbas arguments and prints the result to the standard output. -
The
gcdmethod uses the Euclidean algorithm to calculate the greatest common divisor ofaandb. Ifbis 0, it returnsaas the GCD. Otherwise, it recursively calls itself withband the remainder ofadivided bybas the new arguments. This process continues untilbis 0, at which pointais the GCD of the originalaandb.
Similar Questions
Write a program to find out the Highest Common Factor for given two numbers.
Let's consider all integers in the range from 11 to n𝑛 (inclusive).Among all pairs of distinct integers in this range, find the maximum possible greatest common divisor of integers in pair. Formally, find the maximum value of gcd(a,b)gcd(𝑎,𝑏), where 1≤a<b≤n1≤𝑎<𝑏≤𝑛.The greatest common divisor, gcd(a,b)gcd(𝑎,𝑏), of two positive integers a𝑎 and b𝑏 is the biggest integer that is a divisor of both a𝑎 and b𝑏.InputThe first line contains a single integer t𝑡 (1≤t≤1001≤𝑡≤100) — the number of test cases. The description of the test cases follows.The only line of each test case contains a single integer n𝑛 (2≤n≤1062≤𝑛≤106).OutputFor each test case, output the maximum value of gcd(a,b)gcd(𝑎,𝑏) among all 1≤a<b≤n1≤𝑎<𝑏≤𝑛.
JavaScript: Find the greatest common divisor of two positive numbers
int main(){int x, y, m, n;scanf ("%d %d", &x, &y);/* x > 0 and y > 0 */m = x; n = y;while (m != n){if(m>n)m = m - n;elsen = n - m;}printf("%d", n);} x + y using repeated subtraction x mod y using repeated subtraction the greatest common divisor of x and y the least common multiple of x and y
The extended Euclid’s algorithm determines not only the greatest commondivisor d of two positive integers m and n but also integers (not necessarilypositive) x and y, such that mx + ny = d.a. Look up a description of the extended Euclid’s algorithm (see, e.g., [KnuI,p. 13]) and implement it in the language of your choice.b. Modify your program to find integer solutions to the Diophantine equationax + by = c with any set of integer coefficients a, b, and c
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.