/** 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; }

Microsoft Visual C#
7th Edition
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Joyce, Farrell.
Chapter11: Exception Handling
Section: Chapter Questions
Problem 8RQ
Question

I want the algorithm for the attaced codes. " input , output , process"

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;
}
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 }
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
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.

steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781305480537
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Systems Architecture
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning