-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractical15.java
More file actions
25 lines (22 loc) · 880 Bytes
/
practical15.java
File metadata and controls
25 lines (22 loc) · 880 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//Write a method with following method header. public static int gcd(int num1, int num2)Write a program that prompts the user to enter two integers and compute the gcd of two integers
import java.util.Scanner;
public class practical15 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first integer: ");
int num1 = sc.nextInt();
System.out.print("Enter second integer: ");
int num2 = sc.nextInt();
int result = gcd(num1, num2);
System.out.println("The GCD of " + num1 + " and " + num2 + " is: " + result);
sc.close();
}
public static int gcd(int num1, int num2) {
while (num2 != 0) {
int temp = num2;
num2 = num1 % num2;
num1 = temp;
}
return Math.abs(num1);
}
}