Question

In: Computer Science

Java program: Greatest Common Divisor Write a program which finds the greatest common divisor of two...

Java program:

Greatest Common Divisor

Write a program which finds the greatest common divisor of two natural numbers a and b

Input:  a and b are given in a line separated by a single space.

Output: Output the greatest common divisor of a and b.

Constraints: 1 ≤ a, b ≤ 109

Hint: You can use the following observation:

For integers x and y, if xy, then gcd(x, y) = gcd(y, x%y)

Sample Input 1

54 20

Sample Output 1

2

Sample Input 2

147 105

Sample Output 2

21

Solutions

Expert Solution

Below is the code for the question above in JAVA :

import java.util.Scanner;

public class Main{
static int g;
  
static int gcd(int a, int b)
{
  
for(int j = 1; j <= a && j <= b; j++)
{
if(a%j==0 && b%j==0)
  
g = j;
  
}
  
return g;
}

   public static void main(String[] args)
   {
       Scanner obj1 = new Scanner(System.in);
  
   System.out.println("Enter the value of a and b");
  
   int g=1;
   int a = obj1.nextInt();
   int b = obj1.nextInt();
  
   System.out.printf("%d %d", a,b);
  
   System.out.println(" ");
  
  

System.out.printf("%d", gcd(a,b));
  
   }
}

Screenshot for the output:


Related Solutions

Write a C function gcd that returns the greatest common divisor of two integers. The greatest...
Write a C function gcd that returns the greatest common divisor of two integers. The greatest common divisor (GCD) of two integers is the largest integer that evenly divides each of the two numbers.
Activity 6. Write a Prolog definition of the greatest common divisor of two numbers. Then use...
Activity 6. Write a Prolog definition of the greatest common divisor of two numbers. Then use it to compute gcd(4, 10), gcd(15, 36), and gcd(25, 55). Activity 7. Write a Prolog program to find the last item in a list. give me the screenshot pls
JAVA CLASS Greatest Common Divisor Finder Enter first number: 12 Enter second number: 8 Greatest common...
JAVA CLASS Greatest Common Divisor Finder Enter first number: 12 Enter second number: 8 Greatest common divisor: 4 Continue? (y/n): y Enter first number: 77 Enter second number: 33 Greatest common divisor: 11 Continue? (y/n): y Enter first number: 441 Enter second number: 252 Greatest common divisor: 63 Continue? (y/n): n The formula for finding the greatest common divisor of two positive integers x and y follows the Euclidean algorithm as follows: 1.   Subtract x from y repeatedly until y...
Write the greatest common divisor over the given filed as a linear combination of the given...
Write the greatest common divisor over the given filed as a linear combination of the given polynomials. That is, given f(x) and g(x), find a(x) and b(x) so that d(x) = a(x)f(x) + b(x)g(x), where d(x) is the greatest common divisor of f(x) and g(x). (a) x^10 − x^7 − x^5 + x^3 + x^2 − 1 and x^8 − x^5 − x^3 + 1 over Q. (b) x^5 + x^4 + 2x^2 − x − 1 and x^3 +...
The greatest common divisor of two integers x and y is the largest positive integer that...
The greatest common divisor of two integers x and y is the largest positive integer that evenly divides both. For example, gcd(12,6)=6, gcd(21,14)=7, gcd(31,10)=1, gcd(55,25)=5 Recall that the gcd of two integers is gcd(a,0) = a gcd(a,b) = gcd(b, a%b) Create int gcd(int x, int y). Assume that the two integers are zero or positive. Write the code as a pure function. Put it in a file gcd.c. Of course, you will need to test it. The function Φ(n) counts...
In C++ The greatest common divisor (GCD) of two integers in the largest integer that evenly...
In C++ The greatest common divisor (GCD) of two integers in the largest integer that evenly divides each of the numbers. Write a function called GCD that has a void return type, and accepts 3 parameters (first two by value, third by reference). The function should find the greatest common divisor of the first two numbers, and have the result as its OUTGOING value. Write a main function that asks the users for two integers, and uses your function to...
3. In mathematics, the greatest common divisor (gcd), sometimes known as the greatest common factor (gcf)...
3. In mathematics, the greatest common divisor (gcd), sometimes known as the greatest common factor (gcf) or highest common factor (hcf), of two non-zero integers, is the largest positive integer that divides both numbers. The greatest common divisor of a and b is written as gcd (a, b), or sometimes simply as (a, b). For example, gcd (12, 18) = 6, gcd (−4, 14) = 2 and gcd (5, 0) = 5. Two numbers are called co-prime or relatively prime...
A. 271 and 516 1.  Find the greatest common divisor, d, of the two numbers from part...
A. 271 and 516 1.  Find the greatest common divisor, d, of the two numbers from part A using the Euclidean algorithm. Show and explain all work. 2.  Find all solutions for the congruence ax ? d (mod b) where a and b are the integers from part A and d is the greatest common divisor from part A1. Show and explain all work.
Using python pls!!!!! Write a recursive function gcd(m,n) that returns the greatest common divisor of a...
Using python pls!!!!! Write a recursive function gcd(m,n) that returns the greatest common divisor of a pair of numbers. The gcd of m and n is the largest number that divides both m and n. If one of the numbers is 0, then the gcd is the other number. If m is greater than or equal to n, then the gcd of m and n is the same as the gcd of n and m-n. If n is greater than...
A polynomial in Z[x] is said to be primitive if the greatest common divisor of its...
A polynomial in Z[x] is said to be primitive if the greatest common divisor of its coefficients is 1. Prove the product of two primitive polynomials is primitive. [Hint: Use proof by contradiction.]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT