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.
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.
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)...
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.
Following is an infix expression.                                       &n
Following is an infix expression.                                           ((A ^ B) ^ C ^ M * W / X ) ^ Y ^ Z Convert it into postfix and prefix using stack and verify through binary tree. Evaluate infix, postfix and prefix with the following values. A = 2, B = 2, C = 3, M = 1, W = 4, X = 8, Y = 1, Z = 3
Write a program in C or in Java, that takes an integer value N from the...
Write a program in C or in Java, that takes an integer value N from the command line, generates N random points in the unit square, and computes the distance separating the closest pair of points. A unit square is a square with sides of length 1, at points (0, 0), (0, 1), (1, 0), and (1, 1). If you wish to avoid the command-line processing, you can just assume you will generate a fixed number of points, say between...
( C language only )Generate a function  called randomnum(n,m,neg); where n and m are the lower and...
( C language only )Generate a function  called randomnum(n,m,neg); where n and m are the lower and upper bounds for the random number; the generated number is negative if a variable named neg is true .These numbers should be non-zero with a maximum absolute value of 15 . You must use bitwise arithmetic to calculate the modulus of the random numbers
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT