Question

In: Computer Science

Using Java The C expression m % n yields the remainders of m divided by n....

Using Java

The C expression m % n yields the remainders of m divided by n. Define the greatest common divisor (GCD) of two integers x and y by:

gcd(x, y) = y if (y <= x && x % y == 0)
gcd(x, y) = gcd(y, x) if (x < y)
gcd(x, y) = gcd(y, x % y) otherwise

You will submit a program listing (properly commented) and the output using the following data sets:

x y
32 8
24 15
64 48

Solutions

Expert Solution

import java.lang.*;
import java.util.Scanner;

public class GCD
{
//gcd method using recursion
public static int gcd(int n1, int n2)
{
if (n2 != 0) //recursively call to gcd
return gcd(n2, n1 % n2);
else
return n1;//return n1
}
//driver program
public static void main(String args[])
{
int x,y,g;
//declare scanner object
Scanner scnr = new Scanner(System.in);
//input two numbers
System.out.println("Enter two numbers");
x = scnr.nextInt();
y = scnr.nextInt();
//condition for gcd when y=0
if(y<=x && x%y==0)
g=y;
else
if(x<y) //when x<y
g=gcd(y,x);
else //call gcd when x>y
g=gcd(y,x%y);
//print the GCD
System.out.println("G.C.D of "+x+" and "+y+" is "+g);
}
}

OUTPUT

OR

import java.lang.*;
import java.util.Scanner;

public class GCD
{
//gcd method using recursion
public static int gcd(int n1, int n2)
{
if (n2 != 0) //recursively call to gcd
return gcd(n2, n1 % n2);
else
return n1;//return n1
}
//driver program
public static void main(String args[])
{
int x=32,y=8,g;

//condition for gcd when y=0
if(y<=x && x%y==0)
g=y;
else
if(x<y) //when x<y
g=gcd(y,x);
else //call gcd when x>y
g=gcd(y,x%y);
//print the GCD
System.out.println("G.C.D of "+x+" and "+y+" is "+g);

//set the values of x and y to second data set
   x=24;
y=15;

//condition for gcd when y=0
if(y<=x && x%y==0)
g=y;
else
if(x<y) //when x<y
g=gcd(y,x);
else //call gcd when x>y
g=gcd(y,x%y);
//print the GCD
System.out.println("G.C.D of "+x+" and "+y+" is "+g);


//set the values of x and y to third data set
   x=64;
y=48;

//condition for gcd when y=0
if(y<=x && x%y==0)
g=y;
else
if(x<y) //when x<y
g=gcd(y,x);
else //call gcd when x>y
g=gcd(y,x%y);
//print the GCD
System.out.println("G.C.D of "+x+" and "+y+" is "+g);
}
}

OUTPUT


Related Solutions

Using Java 8. Write a program that reads an expression in postfix notation, builds the expression...
Using Java 8. Write a program that reads an expression in postfix notation, builds the expression tree and prints the expression in prefix and infix notation and evaluates the expression. (Hint use a stack)
Suppose C is a m × n matrix and A is a n × m matrix....
Suppose C is a m × n matrix and A is a n × m matrix. Assume CA = Im (Im is the m × m identity matrix). Consider the n × m system Ax = b. 1. Show that if this system is consistent then the solution is unique. 2. If C = [0 ?5 1 3 0 ?1] and A = [2 ?3   1 ?2    6 10] ,, find x (if it exists) when (a) b =[1...
write a c++ program an expression that determines if an integer, n is a negative four...
write a c++ program an expression that determines if an integer, n is a negative four digit number. write a c++ program an expression that determines if a string, wd, equals "so" ignoring case.
Any java solution for this question ? A state is divided into R*C cities.The government has...
Any java solution for this question ? A state is divided into R*C cities.The government has launched an initiative to find the cities which are dominated by coders. Each city may or may not have coders residing in it. If the city is dominated by coders, it is marked with 1 else it is marked with 0. Two cities are termed as connected cities if they both are dominated by coders and can be reached by moving vertically, horizontally, or...
The Following Java expression: S/10 * 10 + 10 – S = C Show that this...
The Following Java expression: S/10 * 10 + 10 – S = C Show that this expression gives a value of 2 when S contains the int value 78. You must show every step in your working, performing exactly one operation on each line. I have tried to no end to make sense of this and I know there is an order of precedence in the operators but I can't make any sense of it. Could someone please explain this...
Write and test a C implementation of the Mobius function M(n) defined as follows M(n) =...
Write and test a C implementation of the Mobius function M(n) defined as follows M(n) = 1 if n = 1           = 0 if any prime factor is contained in N more than once = (‐1)p if n is the product of p different prime factors Examples M(78) = ‐1     78 = 2 * 3 * 13                   M(34) = 1      34 = 2 * 17                M(45) = 0       45 = 3 * 3 * 5 The first values of M(n)...
FOR JAVA Define a class QuadraticExpression that represents the quadratic expression ax^2 + bx + c:...
FOR JAVA Define a class QuadraticExpression that represents the quadratic expression ax^2 + bx + c: You should provide the following methods (1) default constructor which initalizes all the coefficients to 0 (2) a constructor that takes three parameters public QuadraticExpression(double a, double b, double c) (3) a toString() method that returns the expression as a string. (4) evaluate method that returns the value of the expression at x public double evaluate(double x) (5) set method of a, b, c...
FOR JAVA Define a class QuadraticExpression that represents the quadratic expression ax^2 + bx + c:...
FOR JAVA Define a class QuadraticExpression that represents the quadratic expression ax^2 + bx + c: You should provide the following methods (1) default constructor which initalizes all the coefficients to 0 (2) a constructor that takes three parameters public QuadraticExpression(double a, double b, double c) (3) a toString() method that returns the expression as a string. (4) evaluate method that returns the value of the expression at x public double evaluate(double x) (5) set method of a, b, c...
Using Java, Explain how to implement a functional interface using a lambda expression. You may include...
Using Java, Explain how to implement a functional interface using a lambda expression. You may include small sections of code to illustrate your explanation.
Question 1 a) Determine whether the language {a n b m c n | n >...
Question 1 a) Determine whether the language {a n b m c n | n > 0} is regular or not using pumping Lemma. b) Prove that the language {(ai bn | i, n > 0, i = n or i = 2n} is not regular using the Pumping Lemma.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT