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...
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...
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*
C CODE PLZ! All instructions are in sections of code #include <stdio.h> /* TODO: Define 3...
C CODE PLZ! All instructions are in sections of code #include <stdio.h> /* TODO: Define 3 functions input, gcd and lcm in such a way that the main function below compiles correctly and has the correct behavior. The input function prompts the user to enter a non-negative integer. If the user enters a negative integer, the function prints a "sorry" statement and prompts the user again. It keeps on prompting until the user enters a non-negative number. The input function...
4. Let a, b, c be integers. (a) Prove if gcd(ab, c) = 1, then gcd(a,...
4. Let a, b, c be integers. (a) Prove if gcd(ab, c) = 1, then gcd(a, c) = 1 and gcd(b, c) = 1. (Hint: use the GCD characterization theorem.) (b) Prove if gcd(a, c) = 1 and gcd(b, c) = 1, then gcd(ab, c) = 1. (Hint: you can use the GCD characterization theorem again but you may need to multiply equations.) (c) You have now proved that “gcd(a, c) = 1 and gcd(b, c) = 1 if and...