Question

(Greatest Common Divisor) The greatest common divisor (GCD) of two integers is the largest integer that...

(Greatest Common Divisor) The greatest common divisor (GCD) of two integers is the largest integer that evenly divides each of the numbers. Write a JAVA program that returns the greatest common divisor of two integers.

Homework Answers

Answer #1

JAVA Code to return the greatest common divisor of two integers:

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);
    }
}
Let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a program called gcd that accepts two positive integers in the range of [32,256] as...
Write a program called gcd that accepts two positive integers in the range of [32,256] as parameters and prints the greatest common divisor (GCD) of the two numbers. The GCD of two integers a and b is the largest integer that is a factor of both a and b. One efficient way to compute the GCD of two numbers is to use Euclid’s algorithm, which states the following: GCD (a, b) _ GCD (b, a % b) GCD (a, 0)...
The greatest common divisor c, of a and b, denoted as c = gcd(a, b), is...
The greatest common divisor c, of a and b, denoted as c = gcd(a, b), is the largest number that divides both a and b. One way to write c is as a linear combination of a and b. Then c is the smallest natural number such that c = ax+by for x, y ∈ N. We say that a and b are relatively prime iff gcd(a, b) = 1. Prove that a and n are relatively prime if and...
ARM assembly Code The Euclidean algorithm is a way to find the greatest common divisor of...
ARM assembly Code The Euclidean algorithm is a way to find the greatest common divisor of two positive integers, a and b. First let me show the computations for a=210 and b=45. Divide 210 by 45, and get the result 4 with remainder 30, so 210=4·45+30. Divide 45 by 30, and get the result 1 with remainder 15, so 45=1·30+15. Divide 30 by 15, and get the result 2 with remainder 0, so 30=2·15+0. The greatest common divisor of 210...
Write Java program Lab42.java which takes as input two positive integers m and n and computes...
Write Java program Lab42.java which takes as input two positive integers m and n and computes their least common multiple by calling method lcm(m,n), which in turn calls recursive method gcd(m,n) computing the greatest common divisor of m and n. Recall, that lcm(m,n) can be defined as quotient of m * n and gcd(m,n).
Recursion in Java Write a recursive method that will find the greatest common divisor of two...
Recursion in Java Write a recursive method that will find the greatest common divisor of two numbers. Use %. Example of the math: Finding GCD for 14 and 48: 14 / 48 = 3 42 ----- 6 Remainder Remainder is not yet zero, so we will now divide 14 by 6 6 / 14 = 2 12 ----- 2 Remainder Remainder is not yet zero, so we will now divide 6 by 2 2 / 6 = 3 answer 6...
(a) Show that the following algorithm computes the greatest common divisor g of the positive integers...
(a) Show that the following algorithm computes the greatest common divisor g of the positive integers a and b, together with a solution (u, v) in integers to the equation au + bv = gcd(a, b). 1. Set u = 1, g = a, x = 0, and y = b 2. If y = 0, set v = (g − au)/b and return the values (g, u, v) 3. Divide g by y with remainder, g = qy +...
Two numbers are relatively prime if their greatest common divisor is 1. Show that if a...
Two numbers are relatively prime if their greatest common divisor is 1. Show that if a and b are relatively prime, then there exist integers m and n such that am+bn = 1. (proof by induction preferred)
1. (a) Let a, b and c be positive integers. Prove that gcd(ac, bc) = c...
1. (a) Let a, b and c be positive integers. Prove that gcd(ac, bc) = c x gcd(a, b). (Note that c gcd(a, b) means c times the greatest common division of a and b) (b) What is the greatest common divisor of a − 1 and a + 1? (There are two different cases you need to consider.)
. Let m be a positive integer, find the greatest common divisor of m and m+2
. Let m be a positive integer, find the greatest common divisor of m and m+2
Let m be a positive integer, find the greatest common divisor of m and m +...
Let m be a positive integer, find the greatest common divisor of m and m + 2. *Please go step by step*
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT