/** Find gcd for integers m and n */ public static int gcd(int X, int Y) { int gcd = 1; if (X % Y == 0) return Y; for (int k = Y / 2; k >= 1; k--) { if (X % k == 0 && Y % k == 0) { gcd = k; break; } } return gcd; }
/** Find gcd for integers m and n */ public static int gcd(int X, int Y) { int gcd = 1; if (X % Y == 0) return Y; for (int k = Y / 2; k >= 1; k--) { if (X % k == 0 && Y % k == 0) { gcd = k; break; } } return gcd; }
Chapter11: Exception Handling
Section: Chapter Questions
Problem 8RQ
Related questions
Question
I want the
the codes is written in java
![1 import java.util.Scanner;
2 public class GCD3 {
public static void main(String[] args) {
// Create a Scanner
Scanner sc = new Scanner(System.in);
<terminated> GCD3 [Java Application] C:\Prograr
Enter first integer: 75
Enter second integer: 125
The GCD for 75 and 125 is 25
30
// Prompt the user to enter two integers
System.out.print("Enter first integer: ");
int X = sc.nextInt();
System.out.print("Enter second integer: ");
int Y = sc.nextInt();
7
8
9.
10
11
12
13
14
15
16
170
18
19
20
21
22
23
24
25
26
27
28 }
29
+ Y + " is " + gcd(X, Y));
System.out.println("The GCD for
}
+ X + " and "
/ ** Find gcd for integers m and n */
public static int gcd(int X, int Y) {
int gcd = 1;
if (X % Y == 0) return Y;
for (int k = Y / 2; k >= 1; k--) {
if (X % k == 0 && Y %k == 0) {
gcd = k;
break;
}
}
return gcd;
}](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F84fe8153-915c-413b-a518-dcb4424b4196%2F02ea37d4-ae34-49db-983f-c23de16f9176%2Fxq3kurd_processed.png&w=3840&q=75)
Transcribed Image Text:1 import java.util.Scanner;
2 public class GCD3 {
public static void main(String[] args) {
// Create a Scanner
Scanner sc = new Scanner(System.in);
<terminated> GCD3 [Java Application] C:\Prograr
Enter first integer: 75
Enter second integer: 125
The GCD for 75 and 125 is 25
30
// Prompt the user to enter two integers
System.out.print("Enter first integer: ");
int X = sc.nextInt();
System.out.print("Enter second integer: ");
int Y = sc.nextInt();
7
8
9.
10
11
12
13
14
15
16
170
18
19
20
21
22
23
24
25
26
27
28 }
29
+ Y + " is " + gcd(X, Y));
System.out.println("The GCD for
}
+ X + " and "
/ ** Find gcd for integers m and n */
public static int gcd(int X, int Y) {
int gcd = 1;
if (X % Y == 0) return Y;
for (int k = Y / 2; k >= 1; k--) {
if (X % k == 0 && Y %k == 0) {
gcd = k;
break;
}
}
return gcd;
}
![1 import java.util.Scanner;
2 public class GCD2
/** Main method */
public static void main(String[] args) {
// Create a Scanner
Scanner sc = new Scanner(System. in);
<terminated> GCD2 [Java Application] C:\Program Files\.
Enter first integer: 75
Enter second integer: 125
The GCD for 75 and 125 is 25
40
7
// Prompt the user to enter two integers
System.out.print("Enter first integer: ");
int X = sc.nextInt();
System.out.print("Enter second integer: ");
int Y = sc.nextInt();
8
10
11
12
13
14
15
System.out.println("The GCD for " + X +
and
+ Y +
" is " + gcd(X, Y));
}
16
17
18-
19
20
21
22
23
24
25
26
27
28
/** Return the gcd of two integers */
public static int gcd(int X, int Y) {
int gcd = 1; // Initial gcd is 1
int i = 1; // Possible gcd
while (i <= X && i <= Y) {
if (X % i == 0 && Y % i == 0)
gcd = i; // Update gcd
i++;
}
return gcd; // Return gcd
}
29 }](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F84fe8153-915c-413b-a518-dcb4424b4196%2F02ea37d4-ae34-49db-983f-c23de16f9176%2Fslr03ye_processed.png&w=3840&q=75)
Transcribed Image Text:1 import java.util.Scanner;
2 public class GCD2
/** Main method */
public static void main(String[] args) {
// Create a Scanner
Scanner sc = new Scanner(System. in);
<terminated> GCD2 [Java Application] C:\Program Files\.
Enter first integer: 75
Enter second integer: 125
The GCD for 75 and 125 is 25
40
7
// Prompt the user to enter two integers
System.out.print("Enter first integer: ");
int X = sc.nextInt();
System.out.print("Enter second integer: ");
int Y = sc.nextInt();
8
10
11
12
13
14
15
System.out.println("The GCD for " + X +
and
+ Y +
" is " + gcd(X, Y));
}
16
17
18-
19
20
21
22
23
24
25
26
27
28
/** Return the gcd of two integers */
public static int gcd(int X, int Y) {
int gcd = 1; // Initial gcd is 1
int i = 1; // Possible gcd
while (i <= X && i <= Y) {
if (X % i == 0 && Y % i == 0)
gcd = i; // Update gcd
i++;
}
return gcd; // Return gcd
}
29 }
Expert Solution
![](/static/compass_v2/shared-icons/check-mark.png)
Introduction
GCD (Greatest Common Divisor) of two numbers is the largest number that divides both of them.
Algorithm/Process used:
The algorithm is to Find all prime numbers of both numbers, then find the intersection of all factors present in both numbers. Finally, return the product of elements in the intersection.
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
Recommended textbooks for you
![Microsoft Visual C#](https://www.bartleby.com/isbn_cover_images/9781337102100/9781337102100_smallCoverImage.gif)
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
![EBK JAVA PROGRAMMING](https://www.bartleby.com/isbn_cover_images/9781337671385/9781337671385_smallCoverImage.jpg)
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
![Microsoft Visual C#](https://www.bartleby.com/isbn_cover_images/9781337102100/9781337102100_smallCoverImage.gif)
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
![EBK JAVA PROGRAMMING](https://www.bartleby.com/isbn_cover_images/9781337671385/9781337671385_smallCoverImage.jpg)
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
![EBK JAVA PROGRAMMING](https://www.bartleby.com/isbn_cover_images/9781305480537/9781305480537_smallCoverImage.jpg)
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781305480537
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
![Systems Architecture](https://www.bartleby.com/isbn_cover_images/9781305080195/9781305080195_smallCoverImage.gif)
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning
![C++ Programming: From Problem Analysis to Program…](https://www.bartleby.com/isbn_cover_images/9781337102087/9781337102087_smallCoverImage.gif)
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning