Gcd using recursion
Wed Oct 09 2024 18:20:43 GMT+0000 (Coordinated Universal Time)
Saved by
@wayneinvein
import java.util.Scanner;
public class Gcd {
public static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter two numbers: ");
int a = scanner.nextInt();
int b = scanner.nextInt();
int result = gcd(a, b);
System.out.println("GCD of " + a + " and " + b + " is: " + result);
scanner.close();
}
}
content_copyCOPY
Comments